ExhaustiveMap

Struct ExhaustiveMap 

Source
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>

Source

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.

Source

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.

Source

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

pub const fn len(&self) -> usize

Returns the number of elements in the map.

Always equal to K::INHABITANTS::USIZE.

Source

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.

Source

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.

Source

pub fn swap<Q1: Borrow<K>, Q2: Borrow<K>>(&mut self, k1: Q1, k2: Q2)

Swaps the values at stored at k1 and k2.

Source

pub fn take<Q: Borrow<K>>(&mut self, k: Q) -> V
where V: Default,

Replace the value stored for k with the default value of V, returning the previous stored value.

Source

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");
Source

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.

Source

pub fn values(&self) -> Values<'_, V>

An iterator visiting all values stored in the map, ordered by the keys order provided by Finite.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>>

Source

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>>

Source

pub unsafe fn assume_init(self) -> ExhaustiveMap<K, V>

§Safety

All elements must have been initialized.

Trait Implementations§

Source§

impl<K: Finite, V: Clone> Clone for ExhaustiveMap<K, V>

Source§

fn clone(&self) -> Self

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: Finite + Debug, V: Debug> Debug for ExhaustiveMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Finite, V: Default> Default for ExhaustiveMap<K, V>

Source§

fn default() -> Self

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

impl<K: Finite + Ord, V> From<ExhaustiveMap<K, V>> for BTreeMap<K, V>

Source§

fn from(value: ExhaustiveMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K: Finite, V> From<ExhaustiveMap<K, V>> for Box<[V]>

Source§

fn from(value: ExhaustiveMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K: Finite + Eq + Hash, V, S: BuildHasher + Default> From<ExhaustiveMap<K, V>> for HashMap<K, V, S>

Source§

fn from(value: ExhaustiveMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K: Finite, V: Hash> Hash for ExhaustiveMap<K, V>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<K: Finite, V, Q: Borrow<K>> Index<Q> for ExhaustiveMap<K, V>

Source§

type Output = V

The returned type after indexing.
Source§

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

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

impl<K: Finite, V, Q: Borrow<K>> IndexMut<Q> for ExhaustiveMap<K, V>

Source§

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

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

impl<'a, K: Finite, V> IntoIterator for &'a ExhaustiveMap<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: Finite, V> IntoIterator for &'a mut ExhaustiveMap<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: Finite, V> IntoIterator for ExhaustiveMap<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: Finite, V: Ord> Ord for ExhaustiveMap<K, V>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<K: Finite, V: PartialEq> PartialEq for ExhaustiveMap<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: Finite, V: PartialOrd> PartialOrd for ExhaustiveMap<K, V>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: usize, K: Finite, V> TryFrom<[V; N]> for ExhaustiveMap<K, V>

Source§

type Error = [V; N]

The type returned in the event of a conversion error.
Source§

fn try_from(value: [V; N]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<K: Finite + Ord, V> TryFrom<BTreeMap<K, V>> for ExhaustiveMap<K, V>

Source§

type Error = K

The type returned in the event of a conversion error.
Source§

fn try_from(value: BTreeMap<K, V>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<K: Finite, V> TryFrom<Box<[V]>> for ExhaustiveMap<K, V>

Source§

type Error = Box<[V]>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Box<[V]>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<K: Finite + Eq + Hash, V> TryFrom<HashMap<K, V>> for ExhaustiveMap<K, V>

Source§

type Error = K

The type returned in the event of a conversion error.
Source§

fn try_from(value: HashMap<K, V>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<K: Finite, V> TryFrom<Vec<V>> for ExhaustiveMap<K, V>

Source§

type Error = Vec<V>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Vec<V>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<K: Finite, V: Copy> Copy for ExhaustiveMap<K, V>

Source§

impl<K: Finite, V: Eq> Eq for ExhaustiveMap<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for ExhaustiveMap<K, V>
where <<K as Finite>::INHABITANTS as ArrayLength>::ArrayType<V>: Freeze,

§

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

§

impl<K, V> Send for ExhaustiveMap<K, V>
where V: Send,

§

impl<K, V> Sync for ExhaustiveMap<K, V>
where V: Sync,

§

impl<K, V> Unpin for ExhaustiveMap<K, V>
where <<K as Finite>::INHABITANTS as ArrayLength>::ArrayType<V>: Unpin,

§

impl<K, V> UnwindSafe for ExhaustiveMap<K, V>

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<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> Same for T

Source§

type Output = T

Should always be Self
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.