Skip to main content

OrdinalTotalArrayMap

Struct OrdinalTotalArrayMap 

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

Source

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.

Source

pub fn new(init: impl FnMut(K) -> V) -> Self

Create a new map by initializing each value with a function.

Source

pub fn from_array(array: [V; S]) -> Self

Create a new map from an array of values in ordinal order of keys.

Source

pub const fn len(&self) -> usize

Returns the number of elements in the map, which is always equal to K::ORDINAL_SIZE.

Source

pub const fn is_empty(&self) -> bool

Return true if the map container no elements.

Source

pub fn get<'a>(&'a self, key: &K) -> &'a V

Returns a reference to the value corresponding to the key.

Source

pub fn get_mut<'a>(&'a mut self, key: &K) -> &'a mut V

Returns a mutable reference to the value corresponding to the key.

Source

pub fn as_ref(&self) -> OrdinalTotalArrayMap<K, &V, S>

Return a new map with values as references to values of the original map.

Source

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.

Source

pub fn zip<W>( self, other: OrdinalTotalArrayMap<K, W, S>, ) -> OrdinalTotalArrayMap<K, (V, W), S>

Combine two maps into one.

Source

pub fn map<W>(self, f: impl FnMut(K, V) -> W) -> OrdinalTotalArrayMap<K, W, S>

Map the values of the map.

Source

pub fn map_values<W>( self, f: impl FnMut(V) -> W, ) -> OrdinalTotalArrayMap<K, W, S>

Map the values of the map.

Source

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

Iterate over the map.

Source

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

Iterate over the map mutably.

Source

pub fn keys(&self) -> OrdinalValues<K>

Iterate keys of the map, which is equivalent to iterating all possible values of K.

Source

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.

Source

pub fn values<'a>(&'a self) -> Iter<'a, V>

Iterate values of the map.

Source

pub fn values_array(&self) -> &[V; S]

Values arrays.

Source

pub fn values_mut<'a>(&'a mut self) -> IterMut<'a, V>

Iterate mutable references to values of the map.

Source

pub fn values_array_mut(&mut self) -> &mut [V; S]

Values arrays.

Source

pub fn into_values(self) -> [V; S]

Convert the map into an array of values.

Source

pub fn insert(&mut self, key: K, value: V) -> V

Insert a value into the map, returning the previous value.

Trait Implementations§

Source§

impl<K, V: Clone, const S: usize> Clone for OrdinalTotalArrayMap<K, V, S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Ordinal + Debug, V: Debug, const S: usize> Debug for OrdinalTotalArrayMap<K, V, S>

Source§

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

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

impl<K: Ordinal, V: Default, const S: usize> Default for OrdinalTotalArrayMap<K, V, S>

Source§

fn default() -> Self

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

impl<'a, K: Ordinal, V, const S: usize> Index<&'a K> for OrdinalTotalArrayMap<K, V, S>

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<'a, K: Ordinal, V, const S: usize> IndexMut<&'a K> for OrdinalTotalArrayMap<K, V, S>

Source§

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

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

impl<K: Ordinal, V, const S: usize> IntoIterator for OrdinalTotalArrayMap<K, V, S>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIterArray<K, V, S>

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: Ordinal, V, const S: usize> IntoIterator for &'a OrdinalTotalArrayMap<K, V, S>

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

Auto Trait Implementations§

§

impl<K, V, const S: usize> Freeze for OrdinalTotalArrayMap<K, V, S>
where V: Freeze,

§

impl<K, V, const S: usize> RefUnwindSafe for OrdinalTotalArrayMap<K, V, S>

§

impl<K, V, const S: usize> Send for OrdinalTotalArrayMap<K, V, S>
where V: Send, K: Send,

§

impl<K, V, const S: usize> Sync for OrdinalTotalArrayMap<K, V, S>
where V: Sync, K: Sync,

§

impl<K, V, const S: usize> Unpin for OrdinalTotalArrayMap<K, V, S>
where V: Unpin, K: Unpin,

§

impl<K, V, const S: usize> UnsafeUnpin for OrdinalTotalArrayMap<K, V, S>
where V: UnsafeUnpin,

§

impl<K, V, const S: usize> UnwindSafe for OrdinalTotalArrayMap<K, V, S>
where 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<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.