pub unsafe trait Finite:
Ord
+ Clone
+ Sized {
const COUNT: usize;
// Required methods
fn index_of(value: Self) -> usize;
fn nth(index: usize) -> Option<Self>;
// Provided method
fn iter() -> FiniteIter<Self> ⓘ { ... }
}
Expand description
Provides the number of values for a type, as well as a 1-to-1 mapping between the subset of
integers [0 .. N) and those values. The ordering of integers in this mapping is homomorphic to
the ordering of values according to Ord
(i.e. T::index_of(a) < T::index_of(b)
iff
a < b
).
This trait may be automatically derived.
§Example
use cantor::*;
#[derive(Finite, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
enum MyType {
A,
B(bool),
C(bool, bool)
}
assert_eq!(MyType::COUNT, 7);
assert_eq!(MyType::index_of(MyType::B(false)), 1);
assert_eq!(MyType::nth(4), Some(MyType::C(false, true)));
§Safety
index_of
must return an integer less than COUNT
. nth
must return a non-None
value iff
it is given an integer less than COUNT
.
Required Associated Constants§
Required Methods§
Sourcefn index_of(value: Self) -> usize
fn index_of(value: Self) -> usize
Gets a unique integer representation for the given value. This defines a 1-to-1 mapping
between values of this type and non-negative integers less than Finite::COUNT
.
Provided Methods§
Sourcefn iter() -> FiniteIter<Self> ⓘ
fn iter() -> FiniteIter<Self> ⓘ
Iterates over all of the values of this type.
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.