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>
impl<K: Enumable, V, const N: usize> EnumTable<K, V, N>
Sourcepub const fn new(table: [(Discriminant<K>, V); N]) -> Self
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.
Sourcepub fn new_with_fn(f: impl FnMut(&K) -> V) -> Self
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.
Sourcepub const fn get(&self, variant: &K) -> &V
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.
Sourcepub const fn get_mut(&mut self, variant: &K) -> &mut V
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.
Sourcepub const fn const_set(&mut self, variant: &K, value: V)
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.