pub struct EnumMap<K, V>where
K: Enumerated,
{ /* private fields */ }
Expand description

A key-value map optimized for Enums used as keys.

Abstracts away the need to handle Option on insert/remove operations. It is faster to initialize than EnumTable, because Default value needn’t be cloned for each field.

Examples

Using get and insert functions.

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

let mut map: EnumMap<Letter, u8> = EnumMap::new();
map.insert(Letter::A, 42);
assert_eq!(Some(&42u8), map.get(Letter::A));
map.remove(Letter::A);
assert_eq!(None, map.get(Letter::A));

Using Index and IndexMut syntactic sugar.

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

let mut map: EnumMap<Letter, u8> = EnumMap::new();
map[Letter::A] = Some(42);
assert_eq!(Some(42u8), map[Letter::A]);
assert_eq!(Some(&42u8), map[Letter::A].as_ref());

Implementations§

source§

impl<K, V> EnumMap<K, V>where
K: Enumerated,

source

pub fn new() -> Self

Creates a new EnumMap, with pre-allocated space for all keys of the enum K. With the underlying array righsized, no resizing is further required.

source

pub fn get(&self, key: K) -> Option<&V>

Attemps to obtain a value for given key, returning Some(V) if found, or None if no value has been inserted for given key yet.

Args
  • key - Instance of K, used to look up the corresponding value.
source

pub fn insert(&mut self, key: K, value: V)

Stores given value under the provided key. Overrides any existing value previously set.

Args
  • key - The instance of K the value inserted can be looked up for.
  • values - Value to bind to K.
source

pub fn remove(&mut self, key: K)

Removes value stored under given key. Further get operations are going to return None.

Trait Implementations§

source§

impl<K, V> Default for EnumMap<K, V>where
K: Enumerated,

source§

fn default() -> Self

Constructs a new instance, capable of holding all values of key K without further resizing.

source§

impl<K, V> Index<K> for EnumMap<K, V>where
K: Enumerated,
V: Default,

§

type Output = Option<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, V> IndexMut<K> for EnumMap<K, V>where
K: Enumerated,
V: Default,

source§

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

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

Auto Trait Implementations§

§

impl<K, V> RefUnwindSafe for EnumMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,

§

impl<K, V> Send for EnumMap<K, V>where
K: Send,
V: Send,

§

impl<K, V> Sync for EnumMap<K, V>where
K: Sync,
V: Sync,

§

impl<K, V> Unpin for EnumMap<K, V>where
K: Unpin,

§

impl<K, V> UnwindSafe for EnumMap<K, V>where
K: UnwindSafe,
V: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.