pub unsafe trait Finite: Ord + Clone + Sized {
const COUNT: usize;
fn index_of(value: Self) -> usize;
fn nth(index: usize) -> Option<Self>;
fn iter() -> FiniteIter<Self>ⓘNotable traits for FiniteIter<T>impl<T: Finite> Iterator for FiniteIter<T> type Item = T;
{ ... }
}
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
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
fn iter() -> FiniteIter<Self>ⓘNotable traits for FiniteIter<T>impl<T: Finite> Iterator for FiniteIter<T> type Item = T;
fn iter() -> FiniteIter<Self>ⓘNotable traits for FiniteIter<T>impl<T: Finite> Iterator for FiniteIter<T> type Item = T;
Iterates over all of the values of this type.