pub struct EnumMap<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(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§
Source§impl<K: Enum, V> EnumMap<K, V>
impl<K: Enum, 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;
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;
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> ⓘ
pub fn into_values(self) -> IntoValues<K, 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"]);Source§impl<K: Enum, V: Default> EnumMap<K, V>
impl<K: Enum, 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], "");Source§impl<K: Enum, V> EnumMap<K, V>
impl<K: Enum, V> EnumMap<K, V>
Sourcepub const fn from_array(array: K::Array<V>) -> EnumMap<K, V>
pub const fn from_array(array: K::Array<V>) -> EnumMap<K, V>
Creates an enum map from array.
Sourcepub fn from_fn<F>(cb: F) -> Selfwhere
F: FnMut(K) -> V,
pub fn from_fn<F>(cb: F) -> Selfwhere
F: FnMut(K) -> V,
Create an enum map, where each value is the returned value from cb
using provided enum key.
use enum_map::{enum_map, Enum, EnumMap};
#[derive(Enum, PartialEq, Debug)]
enum Example {
A,
B,
}
let map = EnumMap::from_fn(|k| k == Example::A);
assert_eq!(map, enum_map! { Example::A => true, Example::B => false })Sourcepub fn try_from_fn<F, E>(cb: F) -> Result<Self, E>
pub fn try_from_fn<F, E>(cb: F) -> Result<Self, E>
Create an enum map, where each value is the returned value of
fallible function cb, using provided enum key.
use enum_map::{enum_map, Enum, EnumMap};
#[derive(Enum, PartialEq, Debug)]
enum Example {
A,
B,
}
let map = EnumMap::try_from_fn(|k: Example| {
if k == Example::A {
Ok(4)
} else {
Err("error")
}
});
assert_eq!(map, Err("error"))§Errors
This returns any errors that cb generates.
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns an iterator over enum map.
The iteration order is deterministic, and when using Enum derive it will be the order in which enum variants are declared.
§Examples
use enum_map::{enum_map, Enum};
#[derive(Enum, PartialEq)]
enum E {
A,
B,
C,
}
let map = enum_map! { E::A => 1, E::B => 2, E::C => 3};
assert!(map.iter().eq([(E::A, &1), (E::B, &2), (E::C, &3)]));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 into_array(self) -> K::Array<V>
pub fn into_array(self) -> K::Array<V>
Consumes an enum map and returns the underlying array.
The order of elements is deterministic, and when using Enum derive it will be the order in which enum variants are declared.
§Examples
use enum_map::{enum_map, Enum};
#[derive(Enum, PartialEq)]
enum E {
A,
B,
C,
}
let map = enum_map! { E::A => 1, E::B => 2, E::C => 3};
assert_eq!(map.into_array(), [1, 2, 3]);Sourcepub const fn as_array(&self) -> &K::Array<V>
pub const fn as_array(&self) -> &K::Array<V>
Returns a reference to the underlying array.
The order of elements is deterministic, and when using Enum derive it will be the order in which enum variants are declared.
§Examples
use enum_map::{enum_map, Enum};
#[derive(Enum, PartialEq)]
enum E {
A,
B,
C,
}
let map = enum_map! { E::A => 1, E::B => 2, E::C => 3};
assert_eq!(map.as_array(), &[1, 2, 3]);Sourcepub fn as_mut_array(&mut self) -> &mut K::Array<V>
pub fn as_mut_array(&mut self) -> &mut K::Array<V>
Returns a mutable reference to the underlying array.
The order of elements is deterministic, and when using Enum derive it will be the order in which enum variants are declared.
§Examples
use enum_map::{enum_map, Enum};
#[derive(Enum, PartialEq)]
enum E {
A,
B,
C,
}
let mut map = enum_map! { E::A => 1, E::B => 2, E::C => 3};
map.as_mut_array()[1] = 42;
assert_eq!(map.as_array(), &[1, 42, 3]);Sourcepub fn as_slice(&self) -> &[V]
pub fn as_slice(&self) -> &[V]
Converts an enum map to a slice representing values.
The order of elements is deterministic, and when using Enum derive it will be the order in which enum variants are declared.
§Examples
use enum_map::{enum_map, Enum};
#[derive(Enum, PartialEq)]
enum E {
A,
B,
C,
}
let map = enum_map! { E::A => 1, E::B => 2, E::C => 3};
assert_eq!(map.as_slice(), &[1, 2, 3]);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 map<F, T>(self, f: F) -> EnumMap<K, T>
pub fn map<F, T>(self, f: F) -> EnumMap<K, 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 });Sourcepub fn try_map<F, T, E>(self, f: F) -> Result<EnumMap<K, T>, E>
pub fn try_map<F, T, E>(self, f: F) -> Result<EnumMap<K, T>, E>
Applies fallible function f to each element of enum map returning
an enum map of the same key type as self or the first error
encountered.
§Examples
use enum_map::enum_map;
let a = enum_map! { false => 0, true => 400 };
let b = a.try_map(|_, x| u8::try_from(x));
assert!(b.is_err());§Errors
This returns any errors that f generates.
Trait Implementations§
Source§impl<'a, K: Enum, V: Arbitrary<'a>> Arbitrary<'a> for EnumMap<K, V>
Requires crate feature "arbitrary"
impl<'a, K: Enum, V: Arbitrary<'a>> Arbitrary<'a> for EnumMap<K, V>
Requires crate feature "arbitrary"
Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<EnumMap<K, V>>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<EnumMap<K, V>>
Self from the given unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreimpl<K: Enum, V> Copy for EnumMap<K, V>
Source§impl<'de, K, V> Deserialize<'de> for EnumMap<K, V>
Requires crate feature "serde"
impl<'de, K, V> Deserialize<'de> for EnumMap<K, V>
Requires crate feature "serde"
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
impl<K: Enum, V: Eq> Eq for EnumMap<K, V>
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> Extend<(K, V)> for EnumMap<K, V>
impl<K: Enum, 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)Source§impl<K, V> FromIterator<(K, V)> for EnumMap<K, V>
impl<K, V> FromIterator<(K, V)> for EnumMap<K, V>
Source§impl<'a, K: Enum, V> IntoIterator for &'a EnumMap<K, V>
impl<'a, K: Enum, V> IntoIterator for &'a EnumMap<K, V>
Source§impl<'a, K: Enum, V> IntoIterator for &'a mut EnumMap<K, V>
impl<'a, K: Enum, V> IntoIterator for &'a mut EnumMap<K, V>
Source§impl<K: Enum, V> IntoIterator for EnumMap<K, V>
impl<K: Enum, V> IntoIterator for EnumMap<K, V>
Source§impl<K: Enum, V: Ord> Ord for EnumMap<K, V>
impl<K: Enum, V: Ord> Ord for EnumMap<K, V>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<K: Enum, V: PartialOrd> PartialOrd for EnumMap<K, V>
impl<K: Enum, V: PartialOrd> PartialOrd for EnumMap<K, V>
impl<K: Enum + 'static, V: 'static> Pod for EnumMap<K, V>
Source§impl<K: Enum + Serialize, V: Serialize> Serialize for EnumMap<K, V>
Requires crate feature "serde"
impl<K: Enum + Serialize, V: Serialize> Serialize for EnumMap<K, V>
Requires crate feature "serde"
Source§impl<K: Enum, V> TransparentWrapper<<K as Enum>::Array<V>> for EnumMap<K, V>
impl<K: Enum, V> TransparentWrapper<<K as Enum>::Array<V>> for EnumMap<K, V>
Source§fn wrap_ref(s: &Inner) -> &Self
fn wrap_ref(s: &Inner) -> &Self
Source§fn wrap_mut(s: &mut Inner) -> &mut Self
fn wrap_mut(s: &mut Inner) -> &mut Self
Source§fn wrap_slice(s: &[Inner]) -> &[Self]
fn wrap_slice(s: &[Inner]) -> &[Self]
Source§fn wrap_slice_mut(s: &mut [Inner]) -> &mut [Self]
fn wrap_slice_mut(s: &mut [Inner]) -> &mut [Self]
Source§fn peel_ref(s: &Self) -> &Inner
fn peel_ref(s: &Self) -> &Inner
Source§fn peel_mut(s: &mut Self) -> &mut Inner
fn peel_mut(s: &mut Self) -> &mut Inner
Source§fn peel_slice(s: &[Self]) -> &[Inner]
fn peel_slice(s: &[Self]) -> &[Inner]
Auto Trait Implementations§
impl<K, V> Freeze for EnumMap<K, V>
impl<K, V> RefUnwindSafe for EnumMap<K, V>
impl<K, V> Send for EnumMap<K, V>
impl<K, V> Sync for EnumMap<K, V>
impl<K, V> Unpin for EnumMap<K, V>
impl<K, V> UnsafeUnpin for EnumMap<K, V>
impl<K, V> UnwindSafe for EnumMap<K, V>
Blanket Implementations§
impl<T> AnyBitPattern for Twhere
T: Pod,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
Source§type Bits = T
type Bits = T
Self must have the same layout as the specified Bits except for
the possible invalid bit patterns being checked during
is_valid_bit_pattern.Source§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self.