enum_map_internals/
internal.rs

1/// Enum mapping type
2///
3/// This trait is internally used by `#[derive(Enum)]`. `Enum<T>` is
4/// implemented by any enum type where V is a generic type representing a
5/// value. The purpose of this generic type is to allow providing a value
6/// type for arrays, as Rust currently does not support higher kinded types.
7///
8/// This trait is also implemented by `bool` and `u8`. While `u8` is
9/// strictly speaking not an actual enum, there are good reasons to consider
10/// it like one, as array of `u8` keys is a relatively common pattern.
11pub trait Enum<V>: Sized {
12    /// Representation of an enum map for type `V`, usually an array.
13    type Array;
14    /// Number of possible states the type can have.
15    const POSSIBLE_VALUES: usize;
16    /// Gets a slice from an array type.
17    fn slice(array: &Self::Array) -> &[V];
18    /// Gets a mutable slice from an array type.
19    fn slice_mut(array: &mut Self::Array) -> &mut [V];
20    /// Takes an usize, and returns an element matching `to_usize` function.
21    fn from_usize(value: usize) -> Self;
22    /// Returns an unique identifier for a value within range of `0..POSSIBLE_VALUES`.
23    fn to_usize(self) -> usize;
24    /// Creates an array using a function called for each argument.
25    fn from_function<F: FnMut(Self) -> V>(f: F) -> Self::Array;
26}
27
28impl<T> Enum<T> for bool {
29    type Array = [T; 2];
30    const POSSIBLE_VALUES: usize = 2;
31    #[inline]
32    fn slice(array: &[T; 2]) -> &[T] {
33        array
34    }
35    #[inline]
36    fn slice_mut(array: &mut [T; 2]) -> &mut [T] {
37        array
38    }
39    #[inline]
40    fn from_usize(value: usize) -> Self {
41        match value {
42            0 => false,
43            1 => true,
44            _ => unreachable!(),
45        }
46    }
47    #[inline]
48    fn to_usize(self) -> usize {
49        self as usize
50    }
51    #[inline]
52    fn from_function<F: FnMut(Self) -> T>(mut f: F) -> [T; 2] {
53        [f(false), f(true)]
54    }
55}
56
57impl<T> Enum<T> for u8 {
58    type Array = [T; 256];
59    const POSSIBLE_VALUES: usize = 256;
60    #[inline]
61    fn slice(array: &[T; 256]) -> &[T] {
62        array
63    }
64    #[inline]
65    fn slice_mut(array: &mut [T; 256]) -> &mut [T] {
66        array
67    }
68    #[inline]
69    fn from_usize(value: usize) -> Self {
70        value as u8
71    }
72    #[inline]
73    fn to_usize(self) -> usize {
74        self as usize
75    }
76    #[inline]
77    fn from_function<F: FnMut(Self) -> T>(mut f: F) -> [T; 256] {
78        array![|i| f(i as u8); 256]
79    }
80}