pub struct EnumMap<'a, 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::{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§

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.

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.

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.

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

Trait Implementations§

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

The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.