Struct enum_map::EnumMap

source ·
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>

source

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);
source

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);
source

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>

source

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>

source

pub const fn from_array(array: K::Array) -> EnumMap<K, V>

Creates an enum map from array.

source

pub fn from_fn<F>(cb: F) -> Self
where 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 })
source

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)]));
source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Returns a mutable iterator over enum map.

source

pub const fn len(&self) -> usize

Returns number of elements in enum map.

source

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);
source

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]);
source

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]);
source

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]);
source

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]);
source

pub fn as_mut_slice(&mut self) -> &mut [V]

Converts a mutable enum map to a mutable slice representing values.

source

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§

source§

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>>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

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
source§

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
source§

impl<K: EnumArray<V>, V> Clone for EnumMap<K, V>
where K::Array: Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: EnumArray<V> + Debug, V: Debug> Debug for EnumMap<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K: EnumArray<V>, V: Default> Default for EnumMap<K, V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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"

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap<K, V>
where K: EnumArray<V> + Copy, V: Copy,

source§

fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K: EnumArray<V>, V> Extend<(K, V)> for EnumMap<K, V>

source§

fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K, V> FromIterator<(K, V)> for EnumMap<K, V>
where Self: Default, K: EnumArray<V>,

source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<K: EnumArray<V>, V: Hash> Hash for EnumMap<K, V>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<K: EnumArray<V>, V> Index<K> for EnumMap<K, V>

§

type Output = V

The returned type after indexing.
source§

fn index(&self, key: K) -> &V

Performs the indexing (container[index]) operation. Read more
source§

impl<K: EnumArray<V>, V> IndexMut<K> for EnumMap<K, V>

source§

fn index_mut(&mut self, key: K) -> &mut V

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'a, K: EnumArray<V>, V> IntoIterator for &'a EnumMap<K, V>

§

type Item = (K, &'a V)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, K: EnumArray<V>, V> IntoIterator for &'a mut EnumMap<K, V>

§

type Item = (K, &'a mut V)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K: EnumArray<V>, V> IntoIterator for EnumMap<K, V>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K: EnumArray<V>, V: Ord> Ord for EnumMap<K, V>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<K: EnumArray<V>, V: PartialEq> PartialEq for EnumMap<K, V>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K: EnumArray<V>, V: PartialOrd> PartialOrd for EnumMap<K, V>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<K: EnumArray<V> + Serialize, V: Serialize> Serialize for EnumMap<K, V>

Requires crate feature "serde"

source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
source§

impl<K: EnumArray<V>, V> Copy for EnumMap<K, V>
where K::Array: Copy,

source§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,