Struct enum_map_internals::EnumMap
source · [−]pub struct EnumMap<K: Enum<V>, V> { /* private fields */ }
Expand description
An enum mapping.
This internally uses an array which stores a value for each possible
enum value. To work, it requires implementation of internal (private,
although public due to macro limitations) trait which allows extracting
information about an enum, which can be automatically generated using
#[derive(EnumMap)]
macro.
Additionally, bool
and u8
automatically derives from EnumMap
. While
u8
is not technically an enum, it’s convenient to consider it like one.
In particular, reverse-complement in benchmark game could be using u8
as an enum.
Examples
use enum_map::{enum_map, Enum, EnumMap};
#[derive(Enum)]
enum Example {
A,
B,
C,
}
fn main() {
let mut map = EnumMap::new();
// new initializes map with default values
assert_eq!(map[Example::A], 0);
map[Example::A] = 3;
assert_eq!(map[Example::A], 3);
}
Implementations
sourceimpl<K: Enum<V>, V> EnumMap<K, V>
impl<K: Enum<V>, V> EnumMap<K, V>
sourcepub fn values(&self) -> Values<'_, V>ⓘNotable traits for Values<'a, V>impl<'a, V: 'a> Iterator for Values<'a, V> type Item = &'a V;
pub fn values(&self) -> Values<'_, V>ⓘNotable traits for Values<'a, V>impl<'a, V: 'a> Iterator for Values<'a, V> type Item = &'a V;
An iterator visiting all values. The iterator type is &V
.
Examples
use enum_map::enum_map;
fn main() {
let map = enum_map! { false => 3, true => 4 };
let mut values = map.values();
assert_eq!(values.next(), Some(&3));
assert_eq!(values.next(), Some(&4));
assert_eq!(values.next(), None);
}
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, V>ⓘNotable traits for ValuesMut<'a, V>impl<'a, V: 'a> Iterator for ValuesMut<'a, V> type Item = &'a mut V;
pub fn values_mut(&mut self) -> ValuesMut<'_, V>ⓘNotable traits for ValuesMut<'a, V>impl<'a, V: 'a> Iterator for ValuesMut<'a, V> type Item = &'a mut V;
An iterator visiting all values mutably. The iterator type is &mut V
.
Examples
use enum_map::enum_map;
fn main() {
let mut map = enum_map! { _ => 2 };
for value in map.values_mut() {
*value += 2;
}
assert_eq!(map[false], 4);
assert_eq!(map[true], 4);
}
sourceimpl<K: Enum<V>, V> EnumMap<K, V>
impl<K: Enum<V>, V> EnumMap<K, V>
sourcepub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K: Enum<V>, V> Iterator for Iter<'a, K, V> type Item = (K, &'a V);
pub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K: Enum<V>, V> Iterator for Iter<'a, K, V> type Item = (K, &'a V);
Returns an iterator over enum map.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>ⓘNotable traits for IterMut<'a, K, V>impl<'a, K: Enum<V>, V> Iterator for IterMut<'a, K, V> type Item = (K, &'a mut V);
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>ⓘNotable traits for IterMut<'a, K, V>impl<'a, K: Enum<V>, V> Iterator for IterMut<'a, K, V> type Item = (K, &'a mut V);
Returns a mutable iterator over enum map.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the enum variant set is empty.
This isn’t particularly useful, as there is no real reason to use enum map for enums without variants. However, it is provided for consistency with data structures providing len method (and I will admit, to avoid clippy warnings).
Examples
use enum_map::{Enum, EnumMap};
#[derive(Enum)]
enum Void {}
#[derive(Enum)]
enum SingleVariant {
Variant,
}
fn main() {
assert!(EnumMap::<Void, ()>::new().is_empty());
assert!(!EnumMap::<SingleVariant, ()>::new().is_empty());
}
sourcepub fn swap(&mut self, a: K, b: K)
pub fn swap(&mut self, a: K, b: K)
Swaps two indexes.
Examples
use enum_map::enum_map;
fn main() {
let mut map = enum_map! { false => 0, true => 1 };
map.swap(false, true);
assert_eq!(map[false], 1);
assert_eq!(map[true], 0);
}
sourcepub fn as_mut_slice(&mut self) -> &mut [V]
pub fn as_mut_slice(&mut self) -> &mut [V]
Converts a mutable enum map to a mutable slice representing values.
sourcepub fn as_ptr(&self) -> *const V
pub fn as_ptr(&self) -> *const V
Returns a raw pointer to the enum map’s slice.
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
Examples
use enum_map::{enum_map, EnumMap};
fn main() {
let map = enum_map! { 5 => 42, _ => 0 };
assert_eq!(unsafe { *map.as_ptr().offset(5) }, 42);
}
sourcepub fn as_mut_ptr(&mut self) -> *mut V
pub fn as_mut_ptr(&mut self) -> *mut V
Returns an unsafe mutable pointer to the enum map’s slice.
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
Examples
use enum_map::{enum_map, EnumMap};
fn main() {
let mut map = enum_map! { _ => 0 };
unsafe {
*map.as_mut_ptr().offset(11) = 23
};
assert_eq!(map[11], 23);
}
Trait Implementations
sourceimpl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap<K, V> where
K: Enum<V> + Copy,
V: Copy,
impl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap<K, V> where
K: Enum<V> + Copy,
V: Copy,
sourcefn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<K: Enum<V>, V> Extend<(K, V)> for EnumMap<K, V>
impl<K: Enum<V>, V> Extend<(K, V)> for EnumMap<K, V>
sourcefn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<'a, K: Enum<V>, V> IntoIterator for &'a EnumMap<K, V>
impl<'a, K: Enum<V>, V> IntoIterator for &'a EnumMap<K, V>
sourceimpl<'a, K: Enum<V>, V> IntoIterator for &'a mut EnumMap<K, V>
impl<'a, K: Enum<V>, V> IntoIterator for &'a mut EnumMap<K, V>
sourceimpl<K: Enum<V>, V> IntoIterator for EnumMap<K, V>
impl<K: Enum<V>, V> IntoIterator for EnumMap<K, V>
impl<K: Enum<V>, V> Copy for EnumMap<K, V> where
K::Array: Copy,
impl<K: Enum<V>, V: Eq> Eq for EnumMap<K, V>
Auto Trait Implementations
impl<K, V> RefUnwindSafe for EnumMap<K, V> where
<K as Enum<V>>::Array: RefUnwindSafe,
impl<K, V> Send for EnumMap<K, V> where
<K as Enum<V>>::Array: Send,
impl<K, V> Sync for EnumMap<K, V> where
<K as Enum<V>>::Array: Sync,
impl<K, V> Unpin for EnumMap<K, V> where
<K as Enum<V>>::Array: Unpin,
impl<K, V> UnwindSafe for EnumMap<K, V> where
<K as Enum<V>>::Array: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more