pub struct OrdinalTotalArrayMap<K, V, const S: usize> { /* private fields */ }Expand description
Like OrdinalTotalMap, but without heap allocation.
Due to limitations of stable Rust, ordinal size must be passed as a third type parameter.
§Example
use ordinal_map::map::total::OrdinalTotalArrayMap;
use ordinal_map::Ordinal;
#[derive(Ordinal, Debug)]
enum Color {
Red,
Green,
Blue,
}
let map: OrdinalTotalArrayMap<Color, String, { Color::ORDINAL_SIZE }> =
OrdinalTotalArrayMap::new(|color| format!("{color:?}").to_lowercase());
assert_eq!("green", map[&Color::Green]);Implementations§
Source§impl<K: Ordinal, V, const S: usize> OrdinalTotalArrayMap<K, V, S>
impl<K: Ordinal, V, const S: usize> OrdinalTotalArrayMap<K, V, S>
Sourcepub fn try_new<E>(init: impl FnMut(K) -> Result<V, E>) -> Result<Self, E>
pub fn try_new<E>(init: impl FnMut(K) -> Result<V, E>) -> Result<Self, E>
Create a new map by initializing each value with a function.
Sourcepub fn new(init: impl FnMut(K) -> V) -> Self
pub fn new(init: impl FnMut(K) -> V) -> Self
Create a new map by initializing each value with a function.
Sourcepub fn from_array(array: [V; S]) -> Self
pub fn from_array(array: [V; S]) -> Self
Create a new map from an array of values in ordinal order of keys.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of elements in the map, which is
always equal to K::ORDINAL_SIZE.
Sourcepub fn get<'a>(&'a self, key: &K) -> &'a V
pub fn get<'a>(&'a self, key: &K) -> &'a V
Returns a reference to the value corresponding to the key.
Sourcepub fn get_mut<'a>(&'a mut self, key: &K) -> &'a mut V
pub fn get_mut<'a>(&'a mut self, key: &K) -> &'a mut V
Returns a mutable reference to the value corresponding to the key.
Sourcepub fn as_ref(&self) -> OrdinalTotalArrayMap<K, &V, S>
pub fn as_ref(&self) -> OrdinalTotalArrayMap<K, &V, S>
Return a new map with values as references to values of the original map.
Sourcepub fn as_mut(&mut self) -> OrdinalTotalArrayMap<K, &mut V, S>
pub fn as_mut(&mut self) -> OrdinalTotalArrayMap<K, &mut V, S>
Return a new map with values as mutable references to values of the original map.
Sourcepub fn zip<W>(
self,
other: OrdinalTotalArrayMap<K, W, S>,
) -> OrdinalTotalArrayMap<K, (V, W), S>
pub fn zip<W>( self, other: OrdinalTotalArrayMap<K, W, S>, ) -> OrdinalTotalArrayMap<K, (V, W), S>
Combine two maps into one.
Sourcepub fn map<W>(self, f: impl FnMut(K, V) -> W) -> OrdinalTotalArrayMap<K, W, S>
pub fn map<W>(self, f: impl FnMut(K, V) -> W) -> OrdinalTotalArrayMap<K, W, S>
Map the values of the map.
Sourcepub fn map_values<W>(
self,
f: impl FnMut(V) -> W,
) -> OrdinalTotalArrayMap<K, W, S>
pub fn map_values<W>( self, f: impl FnMut(V) -> W, ) -> OrdinalTotalArrayMap<K, W, S>
Map the values of the map.
Sourcepub fn keys(&self) -> OrdinalValues<K> ⓘ
pub fn keys(&self) -> OrdinalValues<K> ⓘ
Iterate keys of the map, which is equivalent to iterating all possible values of K.
Sourcepub fn into_keys(self) -> OrdinalValues<K> ⓘ
pub fn into_keys(self) -> OrdinalValues<K> ⓘ
Convert the map into an iterator over keys.
This operation is identical to keys,
but added here for consistency with other map implementations.
Sourcepub fn values_array(&self) -> &[V; S]
pub fn values_array(&self) -> &[V; S]
Values arrays.
Sourcepub fn values_mut<'a>(&'a mut self) -> IterMut<'a, V>
pub fn values_mut<'a>(&'a mut self) -> IterMut<'a, V>
Iterate mutable references to values of the map.
Sourcepub fn values_array_mut(&mut self) -> &mut [V; S]
pub fn values_array_mut(&mut self) -> &mut [V; S]
Values arrays.
Sourcepub fn into_values(self) -> [V; S]
pub fn into_values(self) -> [V; S]
Convert the map into an array of values.