EnumMap

Struct EnumMap 

Source
pub struct EnumMap<K: Enumerated, V, const N: usize> { /* private fields */ }
Expand description

A map of enum variants to values. EnumMap is a fixed-size map, where each variant of the enum is mapped to a value. EnumMap is a a zero-cost abstraction over an array, where the index of the array corresponds to the position of the variant in the enum.

Because it is a thin wrapper of an array, it is stack-allocated by default. Simply std::boxed::Boxing it will move it to the heap, at the caller’s discretion.

  • Indexed by enum variants.
  • IndexMut by enum variants.
  • Debug if the enum is Debug.
  • PartialEq if the value is PartialEq. Same for Eq.

Debug and Eq are optional features. They are enabled by default.

§Examples

use enum_collections::{EnumMap, Enumerated, em_default, em};

#[derive(Enumerated)]
pub enum Letter {
   A,
   B,
}


// Indexing and mutation
let mut enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new_default();
assert_eq!(0, enum_map[Letter::A]);
enum_map[Letter::A] = 42;
assert_eq!(42, enum_map[Letter::A]);

// Construction using macros
// (Key type, Value type, Key=>Value pairs)
let enum_map = em!(Letter, i32,  A=>42, B=>24); // All values set explicitly
assert_eq!(42, enum_map[Letter::A]);
assert_eq!(24, enum_map[Letter::B]);

// (Key type, Value type, optional Key=>Value pairs)
let enum_map = em_default!(Letter, i32, A => 42); // Default used for missing values
assert_eq!(42, enum_map[Letter::A]);
assert_eq!(i32::default(), enum_map[Letter::B]);

let enum_map = em_default!(Letter, i32,); // All default
assert_eq!(i32::default(), enum_map[Letter::A]);
assert_eq!(i32::default(), enum_map[Letter::B]);


// Constructor with default values
let enum_map_default = EnumMap::<Letter, i32, { Letter::SIZE }>::new_default();
assert_eq!(0, enum_map_default[Letter::A]);
assert_eq!(0, enum_map_default[Letter::B]);

// Convenience constructor for optional values
let mut enum_map_option = EnumMap::<Letter, Option<i32>, { Letter::SIZE }>::new_option();
assert_eq!(None, enum_map_option[Letter::A]);
assert_eq!(None, enum_map_option[Letter::B]);
enum_map_option[Letter::A] = Some(42);
assert_eq!(Some(42), enum_map_option[Letter::A]);

// Constructor with custom initialization
#[derive(PartialEq, Eq, Debug)]
struct Custom;
let enum_map = EnumMap::<Letter, Custom, { Letter::SIZE }>::new(|| Custom);
assert_eq!(Custom, enum_map[Letter::A]);
assert_eq!(Custom, enum_map[Letter::B]);

// Custom initialization function with enum variant (key) inspection
let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new_inspect(|letter| {
    match letter {
       Letter::A => 42,
       Letter::B => 24,
    }
});
assert_eq!(42, enum_map[Letter::A]);
assert_eq!(24, enum_map[Letter::B]);

// Debug
#[derive(Enumerated, Debug)]
pub enum LetterDebugDerived {
   A,
   B,
}
let enum_map_debug =
    EnumMap::<LetterDebugDerived, i32, { LetterDebugDerived::SIZE }>::new(|| 42);
assert_eq!("{A: 42, B: 42}", format!("{:?}", enum_map_debug));

Implementations§

Source§

impl<K: Enumerated, V: Default, const N: usize> EnumMap<K, V, N>

Source

pub fn new_default() -> Self

Creates a new EnumMap with type’s default values for each variant.

use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated)]
pub enum Letter {
   A,
   B,
}

let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new_default();
assert_eq!(0, enum_map[Letter::A]);
assert_eq!(0, enum_map[Letter::B]);
Source

pub fn clear_set_default(&mut self)

Sets all values to V::default().

use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated)]
pub enum Letter {
  A,
    B,
}
let mut enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
enum_map.iter().for_each(|value| assert_eq!(42, *value));
enum_map.clear_set_default();
enum_map.iter().for_each(|value| assert_eq!(0, *value));
Source§

impl<K: Enumerated, V, const N: usize> EnumMap<K, Option<V>, N>

Source

pub fn new_option() -> Self

Creates a new EnumMap with Option::None set for each variant. Convenience constructor over EnumMap::new for optional values.

use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated)]
pub enum Letter {
   A,
   B,
}

let enum_map = EnumMap::<Letter, Option<i32>, { Letter::SIZE }>::new_option();
assert_eq!(None, enum_map[Letter::A]);
assert_eq!(None, enum_map[Letter::B]);
Source

pub fn clear_set_none(&mut self)

Clears the EnumMap and sets all values to None.

This function iterates over each variant of the EnumMap and sets its value to None.

§Examples
use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated)]
pub enum Letter {
   A,
   B,
}

let mut enum_map = EnumMap::<Letter, Option<i32>, { Letter::SIZE }>::new_option();
enum_map[Letter::A] = Some(10);
enum_map[Letter::B] = Some(20);

enum_map.clear_set_none();
enum_map.iter().for_each(|value| assert_eq!(None, *value));
Source§

impl<K: Enumerated, V, const N: usize> EnumMap<K, V, N>

Source

pub fn new(default: fn() -> V) -> Self

Creates a new EnumMap where value of each variant is produced by the provided function

use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated)]
pub enum Letter {
   A,
   B,
}

let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
assert_eq!(42, enum_map[Letter::A]);
assert_eq!(42, enum_map[Letter::B]);
Source

pub fn set_all(&mut self, val_provider: fn() -> V)

Iterates over each variant of the EnumMap and sets its value to the value provided by the val_provider function.

Unlike Self::new, this function does not allocate a new EnumMap.

§Examples
use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated)]
pub enum Letter {
  A,
  B,
}

let mut enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
enum_map.set_all(|| 24);
enum_map.iter().for_each(|value| assert_eq!(24, *value));
Source

pub fn iter_kv(&self) -> Zip<Iter<'_, K>, Iter<'_, V>>

Iterates over the EnumMap’s key-value pairs.

use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated, Debug)]
pub enum Letter {
   A,
   B,
}

let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
for (_letter, value) in enum_map.iter_kv() {
   assert_eq!(42, *value);
}
Source

pub fn iter(&self) -> Iter<'_, V>

Iterates over the EnumMap’s values.

use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated, Debug)]
pub enum Letter {
   A,
   B,
}

let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
for value in enum_map.iter() {
   assert_eq!(42, *value);
}
Source

pub fn new_inspect(default: impl FnMut(&K) -> V) -> Self

Creates a new EnumMap where value of each variant is produced by the provided function. The function receives the enum variant being initialized for inspection.

use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated)]
pub enum Letter {
  A,
 B,
}

let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new_inspect(|letter| {
   match letter {
      Letter::A => 42,
     Letter::B => 24,
   }
});
assert_eq!(42, enum_map[Letter::A]);
assert_eq!(24, enum_map[Letter::B]);
Source§

impl<K: Enumerated, V: Copy, const N: usize> EnumMap<K, V, N>

Source

pub const fn new_with_all(value: V) -> Self

Creates a new EnumMap where value of each variant is the provided value.

use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated)]
pub enum Letter {
  A,
  B,
}

let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new_with_all(42);
assert_eq!(42, enum_map[Letter::A]);
assert_eq!(42, enum_map[Letter::B]);

Trait Implementations§

Source§

impl<K: Enumerated, V: Clone, const N: usize> Clone for EnumMap<K, V, N>

Implements Clone for EnumMap. Clones the EnumMap by cloning each value. Requires the value to be Clone.

use enum_collections::{EnumMap, Enumerated};

#[derive(Enumerated, Debug)]
pub enum Letter {
    A,
    B,
}
let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
let cloned = enum_map.clone();
assert_eq!(enum_map, cloned);
Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Enumerated + Debug, V: Debug, const N: usize> Debug for EnumMap<K, V, N>

Available on crate feature debug only.

Implements Debug for EnumMap. EnumMap is printed as a map of enum variants to their values.


use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated, Debug)]
enum Letter {
   A,
   B,
}

let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
assert_eq!("{A: 42, B: 42}", format!("{:?}", enum_map));
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Enumerated, V, const N: usize> Index<K> for EnumMap<K, V, N>

Allows indexing of EnumMap.

 use enum_collections::{EnumMap, Enumerated};

#[derive(Enumerated)]
pub enum LetterEqDerived {
    A,
    B,
}

let enum_map = EnumMap::<LetterEqDerived, i32, { LetterEqDerived::SIZE }>::new(|| 42);
assert_eq!(42, enum_map[LetterEqDerived::A]);
assert_eq!(42, enum_map[LetterEqDerived::B]);
Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, key: K) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<K: Enumerated, V, const N: usize> IndexMut<K> for EnumMap<K, V, N>

Allows mutable indexing of EnumMap.

 use enum_collections::{EnumMap, Enumerated};

#[derive(Enumerated)]
pub enum LetterEqDerived {
    A,
    B,
}

let mut enum_map = EnumMap::<LetterEqDerived, i32, { LetterEqDerived::SIZE }>::new_default();
assert_eq!(0, enum_map[LetterEqDerived::A]);
enum_map[LetterEqDerived::A] = 42;
assert_eq!(42, enum_map[LetterEqDerived::A]);
Source§

fn index_mut(&mut self, key: K) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<K: Enumerated, V: PartialEq, const N: usize> PartialEq for EnumMap<K, V, N>

Implements PartialEq for EnumMap. Two enum maps are PartialEq if for each enum variant, the value is the same.

use enum_collections::{EnumMap, Enumerated};

#[derive(Enumerated, Eq, PartialEq)]
pub enum LetterEqDerived {
    A,
    B,
}
let enum_map = EnumMap::<LetterEqDerived, i32, { LetterEqDerived::SIZE }>::new(|| 42);
let same_map = EnumMap::<LetterEqDerived, i32, { LetterEqDerived::SIZE }>::new(|| 42);
assert!(enum_map == same_map);

let different_map = EnumMap::<LetterEqDerived, i32, { LetterEqDerived::SIZE }>::new_default();
assert!(enum_map != different_map);
Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: Enumerated, V: Copy, const N: usize> Copy for EnumMap<K, V, N>

Implements Copy for EnumMap, provided the value type V also implements Copy.


use enum_collections::{EnumMap, Enumerated};
#[derive(Enumerated, Debug)]
pub enum Letter {
  A,
  B,
}
let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
let copied = enum_map;
assert_eq!(enum_map, copied);
Source§

impl<K: Enumerated, V: Eq, const N: usize> Eq for EnumMap<K, V, N>

Marks EnumMap as Eq. Two enum maps are Eq if for each enum variant, the value is the same.

use enum_collections::{EnumMap, Enumerated};

#[derive(Enumerated, Eq, PartialEq)]
pub enum LetterEqDerived {
    A,
    B,
}
let first = EnumMap::<LetterEqDerived, i32, { LetterEqDerived::SIZE }>::new(|| 42);
let second = EnumMap::<LetterEqDerived, i32, { LetterEqDerived::SIZE }>::new(|| 42);
let third = EnumMap::<LetterEqDerived, i32, { LetterEqDerived::SIZE }>::new(|| 42);
// Reflexive
assert!(first == first);
// Symmetric
assert!(first == second);
assert!(second == first);
// Transitive
assert!(second == third);
assert!(first == third);

Auto Trait Implementations§

§

impl<K, V, const N: usize> Freeze for EnumMap<K, V, N>
where V: Freeze,

§

impl<K, V, const N: usize> RefUnwindSafe for EnumMap<K, V, N>

§

impl<K, V, const N: usize> Send for EnumMap<K, V, N>
where V: Send, K: Send,

§

impl<K, V, const N: usize> Sync for EnumMap<K, V, N>
where V: Sync, K: Sync,

§

impl<K, V, const N: usize> Unpin for EnumMap<K, V, N>
where V: Unpin, K: Unpin,

§

impl<K, V, const N: usize> UnwindSafe for EnumMap<K, V, N>
where V: UnwindSafe, K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.