pub struct EnumMap<K, V> where
    K: Enum<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(EnumMap)] macro.

Additionally, bool and u8 automatically derives from EnumMap. 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

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

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

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

Returns an iterator over enum map.

Returns a mutable iterator over enum map.

Returns number of elements in enum map.

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

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

Converts an enum map to a slice representing values.

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Requires crate feature "serde"

Deserialize this value from the given Serde deserializer. Read more

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

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

Extends a collection with exactly one element.

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

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

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

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

Extends a collection with exactly one element.

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

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

Performs the conversion.

Feeds this value into the given Hasher. Read more

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

The returned type after indexing.

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

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

This method tests for !=.

Requires crate feature "serde"

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.