Trait Countable

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

Source

const MAX_VALUE: usize

Maximum value returned by self.as_usize().

Required Methods§

Source

fn as_usize(self) -> usize

Convert self into a usize.

Source

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.

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 Countable for bool

Source§

const MAX_VALUE: usize = 1usize

Source§

fn as_usize(self) -> usize

Source§

fn from_usize(value: usize) -> Self

Source§

impl Countable for ()

Source§

const MAX_VALUE: usize = 0usize

Source§

fn as_usize(self) -> usize

Source§

fn from_usize(_value: usize) -> Self

Implementors§

Source§

impl Countable for BooleanOperator

cbindgen:ignore

Source§

const MAX_VALUE: usize = 7usize