Trait Finite

Source
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§

Source

const COUNT: usize

The number of valid values of this type.

Required Methods§

Source

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.

Source

fn nth(index: usize) -> Option<Self>

Gets the value with the given index as returned by Finite::index_of, or returns None if the index is out of bounds.

Provided Methods§

Source

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.

Implementations on Foreign Types§

Source§

impl Finite for bool

Source§

const COUNT: usize = 2usize

Source§

fn index_of(value: Self) -> usize

Source§

fn nth(index: usize) -> Option<Self>

Source§

impl Finite for u8

Source§

const COUNT: usize = 256usize

Source§

fn index_of(value: Self) -> usize

Source§

fn nth(index: usize) -> Option<Self>

Source§

impl Finite for u16

Source§

const COUNT: usize = 65_536usize

Source§

fn index_of(value: Self) -> usize

Source§

fn nth(index: usize) -> Option<Self>

Source§

impl Finite for ()

Source§

const COUNT: usize = 1usize

Source§

fn index_of(_: Self) -> usize

Source§

fn nth(index: usize) -> Option<Self>

Source§

impl<A: Finite, B: Finite> Finite for (A, B)

Source§

const COUNT: usize

Source§

fn index_of(value: Self) -> usize

Source§

fn nth(index: usize) -> Option<Self>

Source§

impl<T: Finite> Finite for Option<T>

Source§

const COUNT: usize

Source§

fn index_of(value: Self) -> usize

Source§

fn nth(index: usize) -> Option<Self>

Implementors§

Source§

impl<T: BitmapFinite> Finite for BitmapSet<T>

Source§

impl<T: CompressFinite> Finite for Compress<T>

Source§

const COUNT: usize = T::COUNT