pub struct EnumMap<K: EnumArray<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,
}
let mut map = EnumMap::default();
// 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: EnumArray<V>, V> EnumMap<K, V>
impl<K: EnumArray<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;
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;
let mut map = enum_map! { _ => 2 };
for value in map.values_mut() {
*value += 2;
}
assert_eq!(map[false], 4);
assert_eq!(map[true], 4);
sourcepub fn into_values(self) -> IntoValues<K, V>ⓘNotable traits for IntoValues<K, V>impl<K, V> Iterator for IntoValues<K, V> where
K: EnumArray<V>, type Item = V;
pub fn into_values(self) -> IntoValues<K, V>ⓘNotable traits for IntoValues<K, V>impl<K, V> Iterator for IntoValues<K, V> where
K: EnumArray<V>, type Item = V;
K: EnumArray<V>, type Item = V;
Creates a consuming iterator visiting all the values. The map
cannot be used after calling this. The iterator element type
is V
.
Examples
use enum_map::enum_map;
let mut map = enum_map! { false => "hello", true => "goodbye" };
assert_eq!(map.into_values().collect::<Vec<_>>(), ["hello", "goodbye"]);
sourceimpl<K: EnumArray<V>, V: Default> EnumMap<K, V>
impl<K: EnumArray<V>, V: Default> EnumMap<K, V>
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear enum map with default values.
Examples
use enum_map::{Enum, EnumMap};
#[derive(Enum)]
enum Example {
A,
B,
}
let mut enum_map = EnumMap::<_, String>::default();
enum_map[Example::B] = "foo".into();
enum_map.clear();
assert_eq!(enum_map[Example::A], "");
assert_eq!(enum_map[Example::B], "");
sourceimpl<K: EnumArray<V>, V> EnumMap<K, V>
impl<K: EnumArray<V>, V> EnumMap<K, V>
sourcepub fn from_array(array: K::Array) -> EnumMap<K, V>
pub fn from_array(array: K::Array) -> EnumMap<K, V>
Creates an enum map from array.
sourcepub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K: EnumArray<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: EnumArray<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: EnumArray<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: EnumArray<V>, V> Iterator for IterMut<'a, K, V> type Item = (K, &'a mut V);
Returns a mutable iterator over enum map.
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;
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 map<F, T>(self, f: F) -> EnumMap<K, T> where
F: FnMut(K, V) -> T,
K: EnumArray<T>,
pub fn map<F, T>(self, f: F) -> EnumMap<K, T> where
F: FnMut(K, V) -> T,
K: EnumArray<T>,
Returns an enum map with function f
applied to each element in order.
Examples
use enum_map::enum_map;
let a = enum_map! { false => 0, true => 1 };
let b = a.map(|_, x| f64::from(x) + 0.5);
assert_eq!(b, enum_map! { false => 0.5, true => 1.5 });
Trait Implementations
sourceimpl<'a, K: EnumArray<V>, V: Arbitrary<'a>> Arbitrary<'a> for EnumMap<K, V>
impl<'a, K: EnumArray<V>, V: Arbitrary<'a>> Arbitrary<'a> for EnumMap<K, V>
Requires crate feature "arbitrary"
sourcefn arbitrary(u: &mut Unstructured<'a>) -> Result<EnumMap<K, V>>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<EnumMap<K, V>>
Generate an arbitrary value of Self
from the given unstructured data. Read more
sourcefn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Get a size hint for how many bytes out of an Unstructured
this type
needs to construct itself. Read more
sourcefn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Generate an arbitrary value of Self
from the entirety of the given unstructured data. Read more
sourceimpl<'de, K, V> Deserialize<'de> for EnumMap<K, V> where
K: EnumArray<V> + EnumArray<Option<V>> + Deserialize<'de>,
V: Deserialize<'de>,
impl<'de, K, V> Deserialize<'de> for EnumMap<K, V> where
K: EnumArray<V> + EnumArray<Option<V>> + Deserialize<'de>,
V: Deserialize<'de>,
Requires crate feature "serde"
sourcefn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
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: EnumArray<V> + Copy,
V: Copy,
impl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap<K, V> where
K: EnumArray<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: EnumArray<V>, V> Extend<(K, V)> for EnumMap<K, V>
impl<K: EnumArray<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: EnumArray<V>, V> IntoIterator for &'a EnumMap<K, V>
impl<'a, K: EnumArray<V>, V> IntoIterator for &'a EnumMap<K, V>
sourceimpl<'a, K: EnumArray<V>, V> IntoIterator for &'a mut EnumMap<K, V>
impl<'a, K: EnumArray<V>, V> IntoIterator for &'a mut EnumMap<K, V>
sourceimpl<K: EnumArray<V>, V> IntoIterator for EnumMap<K, V>
impl<K: EnumArray<V>, V> IntoIterator for EnumMap<K, V>
sourceimpl<K: EnumArray<V> + Serialize, V: Serialize> Serialize for EnumMap<K, V>
impl<K: EnumArray<V> + Serialize, V: Serialize> Serialize for EnumMap<K, V>
Requires crate feature "serde"
impl<K: EnumArray<V>, V> Copy for EnumMap<K, V> where
K::Array: Copy,
impl<K: EnumArray<V>, V: Eq> Eq for EnumMap<K, V>
Auto Trait Implementations
impl<K, V> RefUnwindSafe for EnumMap<K, V> where
<K as EnumArray<V>>::Array: RefUnwindSafe,
impl<K, V> Send for EnumMap<K, V> where
<K as EnumArray<V>>::Array: Send,
impl<K, V> Sync for EnumMap<K, V> where
<K as EnumArray<V>>::Array: Sync,
impl<K, V> Unpin for EnumMap<K, V> where
<K as EnumArray<V>>::Array: Unpin,
impl<K, V> UnwindSafe for EnumMap<K, V> where
<K as EnumArray<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 · sourcefn borrow_mut(&mut self) -> &mut T
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.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more