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,
}
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: 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;
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);
sourceimpl<K: Enum<V>, V: Default> EnumMap<K, V>
impl<K: Enum<V>, V: Default> EnumMap<K, V>
sourcepub fn new() -> Self
👎 Deprecated since 0.6.5: Please use EnumMap::default instead
pub fn new() -> Self
Please use EnumMap::default instead
Creates an enum map with default values.
Examples
use enum_map::{Enum, EnumMap};
#[derive(Enum)]
enum Example {
A,
}
let enum_map = EnumMap::<_, i32>::default();
assert_eq!(enum_map[Example::A], 0);
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: 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
👎 Deprecated since 0.6.5
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.
Examples
use enum_map::{Enum, EnumMap};
#[derive(Enum)]
enum Void {}
#[derive(Enum)]
enum SingleVariant {
Variant,
}
assert!(EnumMap::<Void, ()>::default().is_empty());
assert!(!EnumMap::<SingleVariant, ()>::default().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;
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
👎 Deprecated since 0.6.4: Please use .as_slice().as_ptr() instead
pub fn as_ptr(&self) -> *const V
Please use .as_slice().as_ptr() instead
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};
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
👎 Deprecated since 0.6.4: Please use .as_mut_slice().as_mut_ptr() instead
pub fn as_mut_ptr(&mut self) -> *mut V
Please use .as_mut_slice().as_mut_ptr() instead
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};
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"
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: 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>
sourceimpl<K: Enum<V> + Serialize, V: Serialize> Serialize for EnumMap<K, V>
impl<K: Enum<V> + Serialize, V: Serialize> Serialize for EnumMap<K, V>
Requires crate feature "serde"
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
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