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§
source§impl<K: EnumArray<V>, V> EnumMap<K, V>
impl<K: EnumArray<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;
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: 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], "");
source§impl<K: EnumArray<V>, V> EnumMap<K, V>
impl<K: EnumArray<V>, V> EnumMap<K, V>
sourcepub const fn from_array(array: K::Array) -> EnumMap<K, V>
pub const fn from_array(array: K::Array) -> 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 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
pub fn into_array(self) -> K::Array
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
pub const fn as_array(&self) -> &K::Array
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
pub fn as_mut_array(&mut self) -> &mut K::Array
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.
Trait Implementations§
source§impl<'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"
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§impl<'de, K, V> Deserialize<'de> for EnumMap<K, V>
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>
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: EnumArray<V>, V> Extend<(K, V)> for EnumMap<K, V>
impl<K: EnumArray<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
)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: EnumArray<V>, V> IntoIterator for &'a EnumMap<K, V>
impl<'a, K: EnumArray<V>, V> IntoIterator for &'a EnumMap<K, V>
source§impl<'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>
source§impl<K: EnumArray<V>, V> IntoIterator for EnumMap<K, V>
impl<K: EnumArray<V>, V> IntoIterator for EnumMap<K, V>
source§impl<K: EnumArray<V>, V: Ord> Ord for EnumMap<K, V>
impl<K: EnumArray<V>, V: Ord> Ord for EnumMap<K, V>
source§impl<K: EnumArray<V>, V: PartialEq> PartialEq for EnumMap<K, V>
impl<K: EnumArray<V>, V: PartialEq> PartialEq for EnumMap<K, V>
source§impl<K: EnumArray<V>, V: PartialOrd> PartialOrd for EnumMap<K, V>
impl<K: EnumArray<V>, V: PartialOrd> PartialOrd for EnumMap<K, V>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<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"