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(Enum)]
macro.
Additionally, bool
and u8
automatically derives from Enum
. 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§
Source§impl<K: Enum<V>, V> EnumMap<K, V>
impl<K: Enum<V>, V> EnumMap<K, V>
Sourcepub fn values(&self) -> Values<'_, V> ⓘ
pub fn values(&self) -> Values<'_, 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> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, 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);
}
Source§impl<K: Enum<V>, V> EnumMap<K, V>
impl<K: Enum<V>, V> EnumMap<K, V>
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§
Source§impl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap<K, V>
impl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap<K, V>
Source§fn 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)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<K: Enum<V>, V> Extend<(K, V)> for EnumMap<K, V>
impl<K: Enum<V>, V> Extend<(K, V)> for EnumMap<K, V>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)