Trait Enum

Source
pub trait Enum<V>: Sized {
    type Array;

    const POSSIBLE_VALUES: usize;

    // Required methods
    fn slice(array: &Self::Array) -> &[V];
    fn slice_mut(array: &mut Self::Array) -> &mut [V];
    fn from_usize(value: usize) -> Self;
    fn to_usize(self) -> usize;
    fn from_function<F: FnMut(Self) -> V>(f: F) -> Self::Array;
}
Expand description

Enum mapping type

This trait is internally used by #[derive(Enum)]. Enum<T> is implemented by any enum type where V is a generic type representing a value. The purpose of this generic type is to allow providing a value type for arrays, as Rust currently does not support higher kinded types.

This trait is also implemented by bool and u8. While u8 is strictly speaking not an actual enum, there are good reasons to consider it like one, as array of u8 keys is a relatively common pattern.

Required Associated Constants§

Source

const POSSIBLE_VALUES: usize

Number of possible states the type can have.

Required Associated Types§

Source

type Array

Representation of an enum map for type V, usually an array.

Required Methods§

Source

fn slice(array: &Self::Array) -> &[V]

Gets a slice from an array type.

Source

fn slice_mut(array: &mut Self::Array) -> &mut [V]

Gets a mutable slice from an array type.

Source

fn from_usize(value: usize) -> Self

Takes an usize, and returns an element matching to_usize function.

Source

fn to_usize(self) -> usize

Returns an unique identifier for a value within range of 0..POSSIBLE_VALUES.

Source

fn from_function<F: FnMut(Self) -> V>(f: F) -> Self::Array

Creates an array using a function called for each argument.

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<T> Enum<T> for bool

Source§

const POSSIBLE_VALUES: usize = 2usize

Source§

type Array = [T; 2]

Source§

fn slice(array: &[T; 2]) -> &[T]

Source§

fn slice_mut(array: &mut [T; 2]) -> &mut [T]

Source§

fn from_usize(value: usize) -> Self

Source§

fn to_usize(self) -> usize

Source§

fn from_function<F: FnMut(Self) -> T>(f: F) -> [T; 2]

Source§

impl<T> Enum<T> for u8

Source§

const POSSIBLE_VALUES: usize = 256usize

Source§

type Array = [T; 256]

Source§

fn slice(array: &[T; 256]) -> &[T]

Source§

fn slice_mut(array: &mut [T; 256]) -> &mut [T]

Source§

fn from_usize(value: usize) -> Self

Source§

fn to_usize(self) -> usize

Source§

fn from_function<F: FnMut(Self) -> T>(f: F) -> [T; 256]

Implementors§