EnumMap

Struct EnumMap 

Source
pub struct EnumMap<K: EnumId, V> { /* private fields */ }
Expand description

A map from an EnumId key to values, implemented using an inline array.

This is similar to a EnumMap and is also “direct”, although that is omitted from the name for conciseness. Implementing the EnumId trait implies that the ids are relatively compact, although this is not a strict requirement.

There is no entry API because the overhead of lookups is very small.

Implementations§

Source§

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

Source

pub fn new() -> Self

Create a new map with no entries.

Source

pub fn new_boxed() -> Box<Self>

Create a new map with no entries, allocating memory on the heap instead of the stack.

Using Box::new(EnumMapDirect::new()) could require moving the underlying table from the stack to the heap, as LLVM can struggle at eliminating copies. This method avoids that copy by always allocating in-place.

Source

pub fn len(&self) -> usize

The number of entries in the map.

Source

pub fn is_empty(&self) -> bool

Return true if this map is empty.

Source

pub fn clear(&mut self)

Clear all entries in the map.

Source

pub fn contains_key(&self, id: impl EquivalentId<K>) -> bool

Check if the specified key is present in the map.

Source

pub fn get(&self, id: impl EquivalentId<K>) -> Option<&V>

Get the value associated with the specified key, or None if missing.

Source

pub fn get_mut(&mut self, id: impl EquivalentId<K>) -> Option<&mut V>

Get a mutable reference to the value associated with the specified key, or None if missing.

Source

pub fn insert(&mut self, id: K, value: V) -> Option<V>

Insert a key and a value, returning the previous value.

Source

pub fn remove(&mut self, id: impl EquivalentId<K>) -> Option<V>

Remove a value associated with the given, returning the previous value ifp resent.

Source

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

Iterate over the key-value pairs in the map.

Guaranteed to be sorted by the integer id of the key.

Source

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

Mutably iterate over the key-value pairs in the map.

Guaranteed to be sorted by the integer id of the key.

Source

pub fn retain(&mut self, func: impl FnMut(K, &mut V) -> bool)

Iterate over the entries in the map, removing entries when the callback returns false.

See also std::collections::HashMap::retain.

Trait Implementations§

Source§

impl<K: Clone + EnumId, V: Clone> Clone for EnumMap<K, V>
where K::Array<Option<V>>: Clone,

Source§

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

Returns a duplicate 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: EnumId, 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: EnumId, V> Default for EnumMap<K, V>

Source§

fn default() -> Self

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

impl<'de, K, V> Deserialize<'de> for EnumMap<K, V>
where K: Deserialize<'de> + EnumId, V: Deserialize<'de>,

Source§

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

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, K: EnumId, V: Clone> Extend<(K, &'a V)> for EnumMap<K, V>

Source§

fn extend<T: IntoIterator<Item = (K, &'a V)>>(&mut self, iter: T)

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

Source§

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

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<'a, K: EnumId, V: Clone> FromIterator<(K, &'a V)> for EnumMap<K, V>

Source§

fn from_iter<I: IntoIterator<Item = (K, &'a V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<K: EnumId, V> FromIterator<(K, V)> for EnumMap<K, V>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, K: EnumId, V> Index<&'a K> for EnumMap<K, V>

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: &'a K) -> &Self::Output

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

impl<K: EnumId, V> Index<K> for EnumMap<K, V>

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: K) -> &Self::Output

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

impl<'a, K: EnumId, V> IndexMut<&'a K> for EnumMap<K, V>

Source§

fn index_mut(&mut self, index: &'a K) -> &mut Self::Output

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

impl<K: EnumId, V> IndexMut<K> for EnumMap<K, V>

Source§

fn index_mut(&mut self, index: K) -> &mut Self::Output

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

impl<'a, K: EnumId, 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: EnumId, 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: EnumId, 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: EnumId, 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, V> Serialize for EnumMap<K, V>
where K: EnumId + Serialize, V: Serialize,

Source§

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

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

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

Auto Trait Implementations§

§

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

§

impl<K, V> RefUnwindSafe for EnumMap<K, V>

§

impl<K, V> Send for EnumMap<K, V>
where <K as EnumId>::Array<Option<V>>: Send,

§

impl<K, V> Sync for EnumMap<K, V>
where <K as EnumId>::Array<Option<V>>: Sync,

§

impl<K, V> Unpin for EnumMap<K, V>
where <K as EnumId>::Array<Option<V>>: Unpin, K: Unpin,

§

impl<K, V> UnwindSafe for EnumMap<K, V>
where <K as EnumId>::Array<Option<V>>: UnwindSafe, K: 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> 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

Checks if this value is equivalent to the given key. 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> 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,