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. This is particularly useful when you want to map
enum variants to specific values without the overhead of a HashMap
.
§Type Parameters
K
: The enumeration type that implements theEnumable
trait. This trait ensures that the enum provides a static array of its variants and a count of these variants.V
: The type of values to be associated with each enum variant.N
: The number of variants in the enum, which should match the length of the static array of variants provided by theEnumable
trait.
§Note
The new
method allows for the creation of an EnumTable
in const
contexts,
but it does not perform compile-time checks. For enhanced compile-time safety
and convenience, it is advisable to use the crate::et
macro or
crate::builder::EnumTableBuilder
, which provide these checks.
§Examples
use enum_table::{EnumTable, Enumable};
#[derive(Enumable)]
enum Color {
Red,
Green,
Blue,
}
// Create an EnumTable using the new_with_fn method
let table = EnumTable::<Color, &'static str, { Color::COUNT }>::new_with_fn(|color| match color {
Color::Red => "Red",
Color::Green => "Green",
Color::Blue => "Blue",
});
// Access values associated with enum variants
assert_eq!(table.get(&Color::Red), &"Red");
assert_eq!(table.get(&Color::Green), &"Green");
assert_eq!(table.get(&Color::Blue), &"Blue");
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 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 fn try_new_with_fn<E>(
f: impl FnMut(&K) -> Result<V, E>,
) -> Result<Self, (K, E)>
pub fn try_new_with_fn<E>( f: impl FnMut(&K) -> Result<V, E>, ) -> Result<Self, (K, E)>
Creates a new EnumTable
using a function that returns a Result
for each variant.
This method applies the provided closure to each variant of the enum. If the closure
returns Ok(value)
for all variants, an EnumTable
is constructed and returned as Ok(Self)
.
If the closure returns Err(e)
for any variant, the construction is aborted and
Err((variant, e))
is returned, where variant
is the enum variant that caused the error.
§Arguments
f
- A closure that takes a reference to an enum variant and returns aResult<V, E>
.
§Returns
Ok(Self)
if all variants succeed.Err((variant, e))
if any variant fails, containing the failing variant and the error.
Sourcepub fn checked_new_with_fn(f: impl FnMut(&K) -> Option<V>) -> Result<Self, K>
pub fn checked_new_with_fn(f: impl FnMut(&K) -> Option<V>) -> Result<Self, K>
Creates a new EnumTable
using a function that returns an Option
for each variant.
This method applies the provided closure to each variant of the enum. If the closure
returns Some(value)
for all variants, an EnumTable
is constructed and returned as Ok(Self)
.
If the closure returns None
for any variant, the construction is aborted and
Err(variant)
is returned, where variant
is the enum variant that caused the failure.
§Arguments
f
- A closure that takes a reference to an enum variant and returns anOption<V>
.
§Returns
Ok(Self)
if all variants succeed.Err(variant)
if any variant fails, containing the failing 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 fn keys(&self) -> impl Iterator<Item = &K>
pub fn keys(&self) -> impl Iterator<Item = &K>
Returns an iterator over references to the keys in the table.
Sourcepub fn values(&self) -> impl Iterator<Item = &V>
pub fn values(&self) -> impl Iterator<Item = &V>
Returns an iterator over references to the values in the table.
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
Returns an iterator over mutable references to the values in the table.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
Returns an iterator over mutable references to the values in the table.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
Returns an iterator over mutable references to the values in the table.
Sourcepub fn map<U, F>(self, f: F) -> EnumTable<K, U, N>where
F: FnMut(V) -> U,
pub fn map<U, F>(self, f: F) -> EnumTable<K, U, N>where
F: FnMut(V) -> U,
Maps the values of the table using a closure, creating a new EnumTable
with the transformed values.
§Arguments
f
- A closure that takes a value and returns a transformed value.
§Examples
use enum_table::{EnumTable, Enumable};
#[derive(Enumable)]
enum Color {
Red,
Green,
Blue,
}
let table = EnumTable::<Color, i32, { Color::COUNT }>::new_with_fn(|color| match color {
Color::Red => 1,
Color::Green => 2,
Color::Blue => 3,
});
let doubled = table.map(|x| x * 2);
assert_eq!(doubled.get(&Color::Red), &2);
assert_eq!(doubled.get(&Color::Green), &4);
assert_eq!(doubled.get(&Color::Blue), &6);
Sourcepub fn into_vec(self) -> Vec<(K, V)>
pub fn into_vec(self) -> Vec<(K, V)>
Converts the EnumTable
into a Vec
of key-value pairs.
§Examples
use enum_table::{EnumTable, Enumable};
#[derive(Enumable, Debug, PartialEq, Copy, Clone)]
enum Color {
Red,
Green,
Blue,
}
let table = EnumTable::<Color, &str, { Color::COUNT }>::new_with_fn(|color| match color {
Color::Red => "red",
Color::Green => "green",
Color::Blue => "blue",
});
let vec = table.into_vec();
assert!(vec.contains(&(Color::Red, "red")));
assert!(vec.contains(&(Color::Green, "green")));
assert!(vec.contains(&(Color::Blue, "blue")));
assert_eq!(vec.len(), 3);
Sourcepub fn try_from_vec(vec: Vec<(K, V)>) -> Result<Self, EnumTableFromVecError<K>>
pub fn try_from_vec(vec: Vec<(K, V)>) -> Result<Self, EnumTableFromVecError<K>>
Creates an EnumTable
from a Vec
of key-value pairs.
Returns an error if the vector doesn’t contain exactly one entry for each enum variant.
§Arguments
vec
- A vector containing key-value pairs for each enum variant.
§Examples
use enum_table::{EnumTable, Enumable};
#[derive(Enumable, Debug, PartialEq, Copy, Clone)]
enum Color {
Red,
Green,
Blue,
}
let vec = vec![
(Color::Red, "red"),
(Color::Green, "green"),
(Color::Blue, "blue"),
];
let table = EnumTable::<Color, &str, { Color::COUNT }>::try_from_vec(vec).unwrap();
assert_eq!(table.get(&Color::Red), &"red");
assert_eq!(table.get(&Color::Green), &"green");
assert_eq!(table.get(&Color::Blue), &"blue");
Source§impl<K: Enumable, V, const N: usize> EnumTable<K, Option<V>, N>
impl<K: Enumable, V, const N: usize> EnumTable<K, Option<V>, N>
Sourcepub const fn new_fill_with_none() -> Self
pub const fn new_fill_with_none() -> Self
Creates a new EnumTable
with None
values for each variant.
Sourcepub fn clear_to_none(&mut self)
pub fn clear_to_none(&mut self)
Clears the table, setting each value to None
.
Source§impl<K: Enumable, V: Copy, const N: usize> EnumTable<K, V, N>
impl<K: Enumable, V: Copy, const N: usize> EnumTable<K, V, N>
pub const fn new_fill_with_copy(value: V) -> Self
Source§impl<K: Enumable, V: Default, const N: usize> EnumTable<K, V, N>
impl<K: Enumable, V: Default, const N: usize> EnumTable<K, V, N>
Sourcepub fn new_fill_with_default() -> Self
pub fn new_fill_with_default() -> Self
Creates a new EnumTable
with default values for each variant.
This method initializes the table with the default value of type V
for each
variant of the enumeration.
Sourcepub fn clear_to_default(&mut self)
pub fn clear_to_default(&mut self)
Clears the table, setting each value to its default.