Struct enum_collections::EnumMap
source · 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,
impl<K, V> EnumMap<K, V>where
K: Enumerated,
sourcepub fn new() -> Self
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.
sourcepub fn get(&self, key: K) -> Option<&V>
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 ofK, used to look up the corresponding value.
Trait Implementations§
source§impl<K, V> Default for EnumMap<K, V>where
K: Enumerated,
impl<K, V> Default for EnumMap<K, V>where
K: Enumerated,
source§impl<K, V> PartialEq for EnumMap<K, V>
impl<K, V> PartialEq for EnumMap<K, V>
impl<K, V> Eq for EnumMap<K, V>
Auto Trait Implementations§
impl<K, V> RefUnwindSafe for EnumMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for EnumMap<K, V>
impl<K, V> Sync for EnumMap<K, V>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more