pub struct EnumMap<K, V> where
K: Enum<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, V> EnumMap<K, V> where
K: Enum<V>,
impl<K, V> EnumMap<K, V> where
K: Enum<V>,
sourcepub fn values(&self) -> Values<'_, V>ⓘNotable traits for Values<'a, V>impl<'a, V> Iterator for Values<'a, V> where
V: 'a, type Item = &'a V;
pub fn values(&self) -> Values<'_, V>ⓘNotable traits for Values<'a, V>impl<'a, V> Iterator for Values<'a, V> where
V: 'a, type Item = &'a V;
V: 'a, 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> Iterator for ValuesMut<'a, V> where
V: 'a, type Item = &'a mut V;
pub fn values_mut(&mut self) -> ValuesMut<'_, V>ⓘNotable traits for ValuesMut<'a, V>impl<'a, V> Iterator for ValuesMut<'a, V> where
V: 'a, type Item = &'a mut V;
V: 'a, 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, V> EnumMap<K, V> where
K: Enum<V>,
impl<K, V> EnumMap<K, V> where
K: Enum<V>,
sourcepub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K, V> Iterator for Iter<'a, K, V> where
K: Enum<V>, type Item = (K, &'a V);
pub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K, V> Iterator for Iter<'a, K, V> where
K: Enum<V>, type Item = (K, &'a V);
K: Enum<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, V> Iterator for IterMut<'a, K, V> where
K: Enum<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, V> Iterator for IterMut<'a, K, V> where
K: Enum<V>, type Item = (K, &'a mut V);
K: Enum<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_slice(&self) -> &[V]ⓘNotable traits for &'_ [u8]impl Read for &'_ [u8]impl Write for &'_ mut [u8]
pub fn as_slice(&self) -> &[V]ⓘNotable traits for &'_ [u8]impl Read for &'_ [u8]impl Write for &'_ mut [u8]
Converts an enum map to a slice representing values.
sourcepub fn as_mut_slice(&mut self) -> &mut [V]ⓘNotable traits for &'_ [u8]impl Read for &'_ [u8]impl Write for &'_ mut [u8]
pub fn as_mut_slice(&mut self) -> &mut [V]ⓘNotable traits for &'_ [u8]impl Read for &'_ [u8]impl Write for &'_ mut [u8]
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<'de, K, V> Deserialize<'de> for EnumMap<K, V> where
K: Enum<V> + Enum<Option<V>> + Deserialize<'de>,
V: Deserialize<'de>,
impl<'de, K, V> Deserialize<'de> for EnumMap<K, V> where
K: Enum<V> + Enum<Option<V>> + Deserialize<'de>,
V: Deserialize<'de>,
Requires crate feature "serde"
sourcepub fn deserialize<D>(
deserializer: D
) -> Result<EnumMap<K, V>, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
pub fn deserialize<D>(
deserializer: D
) -> Result<EnumMap<K, V>, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
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,
sourcepub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (&'a K, &'a V)>,
pub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (&'a K, &'a V)>,
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, V> Extend<(K, V)> for EnumMap<K, V> where
K: Enum<V>,
impl<K, V> Extend<(K, V)> for EnumMap<K, V> where
K: Enum<V>,
sourcepub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (K, V)>,
pub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (K, V)>,
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, V> IntoIterator for &'a mut EnumMap<K, V> where
K: Enum<V>,
impl<'a, K, V> IntoIterator for &'a mut EnumMap<K, V> where
K: Enum<V>,
sourceimpl<K, V> IntoIterator for EnumMap<K, V> where
K: Enum<V>,
impl<K, V> IntoIterator for EnumMap<K, V> where
K: Enum<V>,
sourceimpl<'a, K, V> IntoIterator for &'a EnumMap<K, V> where
K: Enum<V>,
impl<'a, K, V> IntoIterator for &'a EnumMap<K, V> where
K: Enum<V>,
sourceimpl<K, V> Serialize for EnumMap<K, V> where
K: Enum<V> + Serialize,
V: Serialize,
impl<K, V> Serialize for EnumMap<K, V> where
K: Enum<V> + Serialize,
V: Serialize,
Requires crate feature "serde"
sourcepub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
pub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl<K, V> Copy for EnumMap<K, V> where
K: Enum<V>,
<K as Enum<V>>::Array: Copy,
impl<K, V> Eq for EnumMap<K, V> where
K: Enum<V>,
V: Eq,
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
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more