pub unsafe trait Countable: Sized + Copy {
const MAX_VALUE: usize;
// Required methods
fn as_usize(self) -> usize;
fn from_usize(value: usize) -> Self;
}Expand description
Types whose values can be counted, i.e. there is a bijection between the
values of the type and the range 0..=MAX_VALUE.
This is mainly intended to be implemented on enums. In most cases, you can
simply derive it.
§Safety
Countable::as_usize() and Countable::from_usize() must form a
bijection between the values of type Self and 0..=MAX_VALUE, more
formally: For all t: Self it must hold that
Self::from_usize(t.as_usize()) == t.
For all u: usize such that t.as_usize() == u for some t: Self,
Self::from_usize(u).as_usize() == u must be true. Furthermore,
t.as_usize() <= Self::MAX_VALUE must hold.
This trait is marked unsafe because violating any invariant of the above may e.g. result in out-of-bounds accesses.
Required Associated Constants§
Required Methods§
sourcefn from_usize(value: usize) -> Self
fn from_usize(value: usize) -> Self
Convert the given value into an instance of Self.
May panic if an invalid value is passed, or return some default value.