pub unsafe trait Linearize {
type Storage<T>: Storage<Self, T>;
type CopyStorage<T>: CopyStorage<Self, T>
where T: Copy;
const LENGTH: usize;
// Required methods
fn linearize(&self) -> usize;
unsafe fn from_linear_unchecked(linear: usize) -> Self
where Self: Sized;
}
Expand description
Types whose values can be enumerated.
Types that implement this trait define a bijection between themselves and an interval of the natural numbers.
§Safety
Self::Storage<T>
must be[T; Self::LENGTH]
.Self::CopyStorage<T>
must be[T; Self::LENGTH]
.Self::linearize
must be a bijection to[0, Self::LENGTH)
.Self::from_linear_unchecked
must be its inverse.
Note that the bijection implies that a roundtrip through
linearize | from_linear_unchecked
must return a value that is, for all intents and
purposes, indistinguishable from the original value. The details of this depend on
Self
.
Required Associated Constants§
Required Associated Types§
Sourcetype Storage<T>: Storage<Self, T>
type Storage<T>: Storage<Self, T>
[T; Self::LENGTH]
This type exists due to a limitation of the rust type system. In a future version
of this crate, all uses of it will be replaced by [T; Self::LENGTH]
.
Sourcetype CopyStorage<T>: CopyStorage<Self, T>
where
T: Copy
type CopyStorage<T>: CopyStorage<Self, T> where T: Copy
[T; Self::LENGTH]
This type exists due to a limitation of the rust type system. In a future version
of this crate, all uses of it will be replaced by [T; Self::LENGTH]
.
Required Methods§
Sourcefn linearize(&self) -> usize
fn linearize(&self) -> usize
Maps this value to the natural numbers.
This function is a bijection to the interval [0, Self::LENGTH)
.
Sourceunsafe fn from_linear_unchecked(linear: usize) -> Selfwhere
Self: Sized,
unsafe fn from_linear_unchecked(linear: usize) -> Selfwhere
Self: Sized,
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.