Struct EnumMap

Source
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,
}

fn main() {
    let mut map = EnumMap::new();
    // 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>, 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;

fn main() {
    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;

fn main() {
    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§

impl<K: Enum<V>, V: Default> EnumMap<K, V>

Source

pub fn new() -> Self

Creates an enum map with default values.

§Examples
use enum_map::{Enum, EnumMap};

#[derive(Enum)]
enum Example {
    A,
}

fn main() {
    let enum_map = EnumMap::<_, i32>::new();
    assert_eq!(enum_map[Example::A], 0);
}
Source§

impl<K: Enum<V>, V> EnumMap<K, V>

Source

pub fn iter(&self) -> Iter<'_, K, V>

Returns an iterator over enum map.

Source

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

Returns a mutable iterator over enum map.

Source

pub fn len(&self) -> usize

Returns number of elements in enum map.

Source

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. However, it is provided for consistency with data structures providing len method (and I will admit, to avoid clippy warnings).

§Examples
use enum_map::{Enum, EnumMap};

#[derive(Enum)]
enum Void {}

#[derive(Enum)]
enum SingleVariant {
    Variant,
}

fn main() {
    assert!(EnumMap::<Void, ()>::new().is_empty());
    assert!(!EnumMap::<SingleVariant, ()>::new().is_empty());
}
Source

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

Swaps two indexes.

§Examples
use enum_map::enum_map;

fn main() {
    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 as_slice(&self) -> &[V]

Converts an enum map to a slice representing values.

Source

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

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

Source

pub fn as_ptr(&self) -> *const V

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

fn main() {
    let map = enum_map! { 5 => 42, _ => 0 };
    assert_eq!(unsafe { *map.as_ptr().offset(5) }, 42);
}
Source

pub fn as_mut_ptr(&mut self) -> *mut V

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

fn main() {
    let mut map = enum_map! { _ => 0 };
    unsafe {
        *map.as_mut_ptr().offset(11) = 23
    };
    assert_eq!(map[11], 23);
}

Trait Implementations§

Source§

impl<K: Enum<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: Enum<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: Enum<V>, V: Default> Default for EnumMap<K, V>

Source§

fn default() -> Self

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

impl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap<K, V>
where K: Enum<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: Enum<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<F: FnMut(K) -> V, K: Enum<V>, V> From<F> for EnumMap<K, V>

Source§

fn from(f: F) -> Self

Converts to this type from the input type.
Source§

impl<K: Enum<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: Enum<V>, V> Index<K> for EnumMap<K, V>

Source§

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: Enum<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: Enum<V>, V> IntoIterator for &'a EnumMap<K, V>

Source§

type Item = (K, &'a V)

The type of the elements being iterated over.
Source§

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: Enum<V>, V> IntoIterator for &'a mut EnumMap<K, V>

Source§

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

The type of the elements being iterated over.
Source§

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: Enum<V>, V> IntoIterator for EnumMap<K, V>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

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: Enum<V>, V: PartialEq> PartialEq for EnumMap<K, V>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

impl<K: Enum<V>, V: Eq> Eq for EnumMap<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for EnumMap<K, V>
where <K as Enum<V>>::Array: Freeze,

§

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§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.