Struct enum_map::EnumMap[][src]

pub struct EnumMap<K: Enum<V>, V> { /* fields omitted */ }
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

impl<K: Enum<V>, V> EnumMap<K, V>[src]

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;
[src]

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

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;
[src]

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

impl<K: Enum<V>, V: Default> EnumMap<K, V>[src]

pub fn clear(&mut self)[src]

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], "");

impl<K: Enum<V>, V> EnumMap<K, V>[src]

pub fn from_array(array: K::Array) -> EnumMap<K, V>[src]

Creates an enum map from array.

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);
[src]

Returns an iterator over enum map.

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);
[src]

Returns a mutable iterator over enum map.

pub fn len(&self) -> usize[src]

Returns number of elements in enum map.

pub fn swap(&mut self, a: K, b: K)[src]

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

pub fn as_slice(&self) -> &[V][src]

Converts an enum map to a slice representing values.

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

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

Trait Implementations

impl<'a, K: Enum<V>, V: Arbitrary<'a>> Arbitrary<'a> for EnumMap<K, V>[src]

Requires crate feature "arbitrary"

fn arbitrary(u: &mut Unstructured<'a>) -> Result<EnumMap<K, V>>[src]

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

fn size_hint(depth: usize) -> (usize, Option<usize>)[src]

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>[src]

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

impl<K: Enum<V>, V> Clone for EnumMap<K, V> where
    K::Array: Clone
[src]

fn clone(&self) -> Self[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<K: Enum<V> + Debug, V: Debug> Debug for EnumMap<K, V>[src]

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

Formats the value using the given formatter. Read more

impl<K: Enum<V>, V: Default> Default for EnumMap<K, V>[src]

fn default() -> Self[src]

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

impl<'de, K, V> Deserialize<'de> for EnumMap<K, V> where
    K: Enum<V> + Enum<Option<V>> + Deserialize<'de>,
    V: Deserialize<'de>, 
[src]

Requires crate feature "serde"

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

Deserialize this value from the given Serde deserializer. Read more

impl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap<K, V> where
    K: Enum<V> + Copy,
    V: Copy
[src]

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

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

fn extend_one(&mut self, item: A)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

fn extend_reserve(&mut self, additional: usize)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

impl<K: Enum<V>, V> Extend<(K, V)> for EnumMap<K, V>[src]

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

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

fn extend_one(&mut self, item: A)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

fn extend_reserve(&mut self, additional: usize)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

impl<K: Enum<V>, V: Hash> Hash for EnumMap<K, V>[src]

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

Feeds this value into the given Hasher. Read more

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

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

impl<K: Enum<V>, V> Index<K> for EnumMap<K, V>[src]

type Output = V

The returned type after indexing.

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

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

impl<K: Enum<V>, V> IndexMut<K> for EnumMap<K, V>[src]

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

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

impl<'a, K: Enum<V>, V> IntoIterator for &'a EnumMap<K, V>[src]

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?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

impl<'a, K: Enum<V>, V> IntoIterator for &'a mut EnumMap<K, V>[src]

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?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

impl<K: Enum<V>, V> IntoIterator for EnumMap<K, V>[src]

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?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

impl<K: Enum<V>, V: PartialEq> PartialEq<EnumMap<K, V>> for EnumMap<K, V>[src]

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

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl<K: Enum<V> + Serialize, V: Serialize> Serialize for EnumMap<K, V>[src]

Requires crate feature "serde"

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

Serialize this value into the given Serde serializer. Read more

impl<K: Enum<V>, V> Copy for EnumMap<K, V> where
    K::Array: Copy
[src]

impl<K: Enum<V>, V: Eq> Eq for EnumMap<K, V>[src]

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

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