Struct enum_map::EnumMap
[−]
[src]
pub struct EnumMap<K: Internal<V>, V> { /* fields omitted */ }
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)]
from enum_map_derive
crate.
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
extern crate enum_map; #[macro_use] extern crate enum_map_derive; use enum_map::EnumMap; #[derive(EnumMap)] 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); }
Methods
impl<K: Internal<V>, V: Default> EnumMap<K, V> where
K::Array: Default,
[src]
K::Array: Default,
fn new() -> Self
Creates an enum map with default values.
extern crate enum_map; #[macro_use] extern crate enum_map_derive; use enum_map::EnumMap; #[derive(EnumMap)] enum Example { A, } fn main() { let enum_map = EnumMap::<_, i32>::new(); assert_eq!(enum_map[Example::A], 0); }
impl<K: Internal<V>, V> EnumMap<K, V>
[src]
fn len(&self) -> usize
Returns number of elements in enum map.
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
extern crate enum_map; #[macro_use] extern crate enum_map_derive; use enum_map::EnumMap; #[derive(EnumMap)] enum Void {} #[derive(EnumMap)] enum SingleVariant { Variant, } fn main() { assert_eq!(EnumMap::<Void, ()>::new().is_empty(), true); assert_eq!(EnumMap::<SingleVariant, ()>::new().is_empty(), false); }
fn iter(&self) -> Iter<K, V>
Returns an iterator over enum map.
fn iter_mut(&mut self) -> IterMut<K, V>
Returns a mutable iterator over enum map.
fn swap(&mut self, a: K, b: K)
Swaps two indexes.
Examples
#[macro_use] extern crate 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); }
Trait Implementations
impl<K: Internal<V>, V> Clone for EnumMap<K, V> where
K::Array: Clone,
[src]
K::Array: Clone,
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<K: Internal<V>, V> Copy for EnumMap<K, V> where
K::Array: Copy,
[src]
K::Array: Copy,
impl<K: Internal<V>, V> PartialEq for EnumMap<K, V> where
K::Array: PartialEq,
[src]
K::Array: PartialEq,
fn eq(&self, other: &Self) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<K: Internal<V>, V> Eq for EnumMap<K, V> where
K::Array: Eq,
[src]
K::Array: Eq,
impl<K: Internal<V>, V> Hash for EnumMap<K, V> where
K::Array: Hash,
[src]
K::Array: Hash,
fn hash<H: Hasher>(&self, state: &mut H)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<K: Internal<V>, V> Default for EnumMap<K, V> where
K::Array: Default,
[src]
K::Array: Default,
impl<K: Internal<V>, V> Index<K> for EnumMap<K, V>
[src]
type Output = V
The returned type after indexing
fn index(&self, key: K) -> &V
The method for the indexing (container[index]
) operation
impl<K: Internal<V>, V> IndexMut<K> for EnumMap<K, V>
[src]
fn index_mut(&mut self, key: K) -> &mut V
The method for the mutable indexing (container[index]
) operation
impl<'a, K: Internal<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
Creates an iterator from a value. Read more
impl<'a, K: Internal<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
Creates an iterator from a value. Read more
impl<K: Debug + Internal<V>, V: Debug> Debug for EnumMap<K, V> where
K::Array: Debug,
[src]
K::Array: Debug,
impl<F: FnMut(K) -> V, K: Internal<V>, V> From<F> for EnumMap<K, V>
[src]
fn from(f: F) -> Self
Performs the conversion.