Skip to main content

EnumMap

Struct EnumMap 

Source
pub struct EnumMap<K, V>
where K: EnumArray<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, V> EnumMap<K, V>
where K: EnumArray<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, V> EnumMap<K, V>
where K: EnumArray<V>, V: Default,

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, V> EnumMap<K, V>
where K: EnumArray<V>,

Source

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

Creates an enum map from array.

Source

pub fn from_fn<F>(cb: F) -> EnumMap<K, V>
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 as EnumArray<V>>::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 as EnumArray<V>>::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 as EnumArray<V>>::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<K, V> Clone for EnumMap<K, V>
where K: EnumArray<V>, <K as EnumArray<V>>::Array: Clone,

Source§

fn clone(&self) -> EnumMap<K, V>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

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

Source§

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

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

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

Source§

fn default() -> EnumMap<K, V>

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: D, ) -> Result<EnumMap<K, V>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

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>(&mut self, iter: I)
where I: IntoIterator<Item = (&'a K, &'a V)>,

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> Extend<(K, V)> for EnumMap<K, V>
where K: EnumArray<V>,

Source§

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

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 EnumMap<K, V>: Default, K: EnumArray<V>,

Source§

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

Creates a value from an iterator. Read more
Source§

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

Source§

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

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, V> Index<K> for EnumMap<K, V>
where K: EnumArray<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, V> IndexMut<K> for EnumMap<K, V>
where K: EnumArray<V>,

Source§

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

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

impl<'a, K, V> IntoIterator for &'a EnumMap<K, V>
where K: EnumArray<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) -> <&'a EnumMap<K, V> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V> IntoIterator for &'a mut EnumMap<K, V>
where K: EnumArray<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) -> <&'a mut EnumMap<K, V> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V> IntoIterator for EnumMap<K, V>
where K: EnumArray<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) -> <EnumMap<K, V> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

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

Source§

fn cmp(&self, other: &EnumMap<K, V>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

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

Source§

fn eq(&self, other: &EnumMap<K, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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, V> PartialOrd for EnumMap<K, V>
where K: EnumArray<V>, V: PartialOrd,

Source§

fn partial_cmp(&self, other: &EnumMap<K, V>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Requires crate feature "serde"

Source§

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

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

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

Source§

impl<K, V> Eq for EnumMap<K, V>
where K: EnumArray<V>, V: Eq,

Auto Trait Implementations§

§

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

§

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> UnsafeUnpin for EnumMap<K, V>
where <K as EnumArray<V>>::Array: UnsafeUnpin,

§

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> 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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DebugExt<T> for T
where T: Debug,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<A> DynCastExt for A

Source§

fn dyn_cast<T>( self, ) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source>
where A: DynCastExtHelper<T>, T: ?Sized,

Use this to cast from one trait object type to another. Read more
Source§

fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target
where A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>, T: ?Sized,

Use this to upcast a trait to one of its supertraits. Read more
Source§

fn dyn_cast_adv<F, T>( self, ) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source>
where A: DynCastExtAdvHelper<F, T>, F: ?Sized, T: ?Sized,

Use this to cast from one trait object type to another. This method is more customizable than the dyn_cast method. Here you can also specify the “source” trait from which the cast is defined. This can for example allow using casts from a supertrait of the current trait object. Read more
Source§

fn dyn_cast_with_config<C>( self, ) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source>

Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> OrdExt<T> for T
where T: Ord + Clone,

Source§

fn update_max(&mut self, new: &T)

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>

Source§

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

Source§

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

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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

Source§

impl<T> RuleType for T
where T: Copy + Debug + Eq + Hash + Ord,