Struct EnumTable

Source
pub struct EnumTable<K: Enumable, V, const N: usize> { /* private fields */ }
Expand description

A table that associates each variant of an enumeration with a value.

EnumTable is a generic struct that uses an enumeration as keys and stores associated values. It provides constant-time access to the values based on the enumeration variant.

Implementations§

Source§

impl<K: Enumable, V, const N: usize> EnumTable<K, V, N>

Source

pub const fn new(table: [(Discriminant<K>, V); N]) -> Self

Creates a new EnumTable with the given table of discriminants and values.

§Arguments
  • table - An array of tuples where each tuple contains a discriminant of an enumeration variant and its associated value.
Source

pub fn new_with_fn(f: impl FnMut(&K) -> V) -> Self

Create a new EnumTable with a function that takes a variant and returns a value. If you want to define it in const, use crate::et macro Creates a new EnumTable using a function to generate values for each variant.

§Arguments
  • f - A function that takes a reference to an enumeration variant and returns a value to be associated with that variant.
Source

pub const fn get(&self, variant: &K) -> &V

Returns a reference to the value associated with the given enumeration variant.

§Arguments
  • variant - A reference to an enumeration variant.
Source

pub const fn get_mut(&mut self, variant: &K) -> &mut V

Returns a mutable reference to the value associated with the given enumeration variant.

§Arguments
  • variant - A reference to an enumeration variant.
Source

pub const fn const_set(&mut self, variant: &K, value: V)

const function is not callable drop. So, we use forget to avoid calling drop. Careful, not to call drop on the old value. Sets the value associated with the given enumeration variant in a constant context.

§Arguments
  • variant - A reference to an enumeration variant.
  • value - The new value to associate with the variant.
§Safety

This method uses std::mem::forget to avoid calling drop on the old value. Be careful not to call drop on the old value manually.

Source

pub fn set(&mut self, variant: &K, value: V)

Sets the value associated with the given enumeration variant.

§Arguments
  • variant - A reference to an enumeration variant.
  • value - The new value to associate with the variant.
Source

pub const fn len(&self) -> usize

Returns the number of generic N

Source

pub const fn is_empty(&self) -> bool

Returns false since the table is never empty.

Trait Implementations§

Source§

impl<K: Clone + Enumable, V: Clone, const N: usize> Clone for EnumTable<K, V, N>

Source§

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

Returns a copy 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: Debug + Enumable, V: Debug, const N: usize> Debug for EnumTable<K, V, N>

Source§

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

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

impl<K: Enumable, V: Default, const N: usize> Default for EnumTable<K, V, N>

Source§

fn default() -> Self

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

impl<K: Enumable, V, const N: usize> Index<K> for EnumTable<K, V, N>

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<K: Enumable, V, const N: usize> IndexMut<K> for EnumTable<K, V, N>

Source§

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

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

impl<K: Copy + Enumable, V: Copy, const N: usize> Copy for EnumTable<K, V, N>

Auto Trait Implementations§

§

impl<K, V, const N: usize> Freeze for EnumTable<K, V, N>

§

impl<K, V, const N: usize> RefUnwindSafe for EnumTable<K, V, N>

§

impl<K, V, const N: usize> Send for EnumTable<K, V, N>
where V: Send,

§

impl<K, V, const N: usize> Sync for EnumTable<K, V, N>
where V: Sync,

§

impl<K, V, const N: usize> Unpin for EnumTable<K, V, N>
where V: Unpin,

§

impl<K, V, const N: usize> UnwindSafe for EnumTable<K, V, N>

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.