pub struct ExhaustiveMap<K: Finite, V> { /* private fields */ }Expand description
A map which is guaranteed to always contain a value for each possible key of
type K.
use exhaustive_map::ExhaustiveMap;
let mut map = ExhaustiveMap::<u8, u16>::from_fn(|i| i as u16 + 100);
assert_eq!(map.len(), 256);
assert_eq!(map[3], 103);
map[7] = 9999;
assert_eq!(map[7], 9999);
map.swap(7, 3);
assert_eq!(map[3], 9999);
assert_eq!(map[7], 103);§Layout
The layout of ExhaustiveMap<K, V> is guaranteed to be the same as [V; N]
where N is the number of inhabitants of type K.
assert_eq!(size_of::<ExhaustiveMap<u8, bool>>(), 256);Implementations§
Source§impl<K: Finite, V> ExhaustiveMap<K, V>
impl<K: Finite, V> ExhaustiveMap<K, V>
Sourcepub fn from_fn(f: impl FnMut(K) -> V) -> Self
pub fn from_fn(f: impl FnMut(K) -> V) -> Self
Creates a map by providing a mapping function from K to V.
Similar to array::from_fn.
Sourcepub fn try_from_fn<E>(f: impl FnMut(K) -> Result<V, E>) -> Result<Self, E>
pub fn try_from_fn<E>(f: impl FnMut(K) -> Result<V, E>) -> Result<Self, E>
Tries to create a map by providing a mapping function from K to
Result<V, E>.
§Errors
Returns the first error if any of the mappings fails.
Sourcepub fn from_usize_fn(f: impl FnMut(usize) -> V) -> Self
pub fn from_usize_fn(f: impl FnMut(usize) -> V) -> Self
Creates a map by providing a mapping function from usize to V.
The map is filled according to the Finite implementation of K.
use exhaustive_map::{ExhaustiveMap, Finite};
#[derive(Finite, Debug)]
enum Color {
Red,
Green,
Blue,
}
let map = ExhaustiveMap::from_usize_fn(|i| i);
assert_eq!(map[Color::Red], 0);
assert_eq!(map[Color::Green], 1);
assert_eq!(map[Color::Blue], 2);Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of elements in the map.
Always equal to K::INHABITANTS::USIZE.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if the map contains no elements.
The map can only be empty if K::INHABITANTS is zero,
meaning the type K is uninhabitable.
Sourcepub fn replace<Q: Borrow<K>>(&mut self, k: Q, v: V) -> V
pub fn replace<Q: Borrow<K>>(&mut self, k: Q, v: V) -> V
Replace the value stored for k with v, returning the previous stored
value.
Sourcepub fn swap<Q1: Borrow<K>, Q2: Borrow<K>>(&mut self, k1: Q1, k2: Q2)
pub fn swap<Q1: Borrow<K>, Q2: Borrow<K>>(&mut self, k1: Q1, k2: Q2)
Swaps the values at stored at k1 and k2.
Sourcepub fn take<Q: Borrow<K>>(&mut self, k: Q) -> Vwhere
V: Default,
pub fn take<Q: Borrow<K>>(&mut self, k: Q) -> Vwhere
V: Default,
Replace the value stored for k with the default value of V,
returning the previous stored value.
Sourcepub fn map_values<U>(self, f: impl FnMut(V) -> U) -> ExhaustiveMap<K, U>
pub fn map_values<U>(self, f: impl FnMut(V) -> U) -> ExhaustiveMap<K, U>
Change the values of the stored values via a mapping function.
use exhaustive_map::ExhaustiveMap;
let bool_to_int = ExhaustiveMap::from_fn(|k| if k { 1 } else { 0 });
let bool_to_int_string = bool_to_int.map_values(|v| v.to_string());
assert_eq!(bool_to_int_string[false], "0");
assert_eq!(bool_to_int_string[true], "1");Sourcepub fn keys() -> IterAll<K> ⓘ
pub fn keys() -> IterAll<K> ⓘ
An iterator visiting all keys in the order provided by Finite.
This creates new keys by calling K::from_usize
for each key.
Sourcepub fn values(&self) -> Values<'_, V> ⓘ
pub fn values(&self) -> Values<'_, V> ⓘ
An iterator visiting all values stored in the map, ordered by the keys
order provided by Finite.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, V> ⓘ
A mutable iterator visiting all values stored in the map, ordered by the
keys order provided by Finite.
Sourcepub fn into_values(self) -> IntoValues<V, K::INHABITANTS> ⓘ
pub fn into_values(self) -> IntoValues<V, K::INHABITANTS> ⓘ
Creates a consuming iterator visiting all the values, ordered by the
keys order provided by Finite. The map cannot be used after
calling this.
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
An iterator visiting all entries stored in the map, ordered by the keys
order provided by Finite.
This creates new keys by calling K::from_usize
for each key.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
A mutable iterator visiting all entries stored in the map, ordered by
the keys order provided by Finite.
This creates new keys by calling K::from_usize
for each key.
Sourcepub fn new_uninit() -> ExhaustiveMap<K, MaybeUninit<V>>
pub fn new_uninit() -> ExhaustiveMap<K, MaybeUninit<V>>
Creates a map with MaybeUninit values.
After every value have been initialized
assume_init can be called to obtain
a map with values of type V.
Source§impl<K: Finite, V> ExhaustiveMap<K, Option<V>>
impl<K: Finite, V> ExhaustiveMap<K, Option<V>>
Sourcepub fn try_unwrap_values(
self,
) -> Result<ExhaustiveMap<K, V>, ExhaustiveMap<K, Option<V>>>
pub fn try_unwrap_values( self, ) -> Result<ExhaustiveMap<K, V>, ExhaustiveMap<K, Option<V>>>
Tries to convert an ExhaustiveMap<K, Option<V>> to an
ExhaustiveMap<K, V>.
§Errors
If any of the values are None, this returns Err containing the input
map.
Source§impl<K: Finite, V> ExhaustiveMap<K, MaybeUninit<V>>
impl<K: Finite, V> ExhaustiveMap<K, MaybeUninit<V>>
Sourcepub unsafe fn assume_init(self) -> ExhaustiveMap<K, V>
pub unsafe fn assume_init(self) -> ExhaustiveMap<K, V>
§Safety
All elements must have been initialized.