pub struct EnumTable<K: Enumerable, V, const N: usize> { /* private fields */ }Expand description
A fixed-size table holding one V per variant of K.
A value always exists for every variant, so Self::get returns &V directly
rather than Option<&V>; use EnumTable<K, Option<V>, N> to allow an absent value.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone)]
enum Color {
Red,
Green,
Blue,
}
let table = EnumTable::<Color, &'static str, { Color::COUNT }>::from_fn(|color| match color {
Color::Red => "Red",
Color::Green => "Green",
Color::Blue => "Blue",
});
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: Enumerable, V, const N: usize> EnumTable<K, V, N>
impl<K: Enumerable, V, const N: usize> EnumTable<K, V, N>
Sourcepub fn keys(&self) -> Iter<'_, K> ⓘ
pub fn keys(&self) -> Iter<'_, K> ⓘ
Returns an iterator over references to the keys in the table.
Sourcepub fn values(&self) -> Iter<'_, V> ⓘ
pub fn values(&self) -> Iter<'_, V> ⓘ
Returns an iterator over references to the values in the table.
Sourcepub fn values_mut(&mut self) -> IterMut<'_, V> ⓘ
pub fn values_mut(&mut self) -> IterMut<'_, V> ⓘ
Returns an iterator over mutable references to the values in the table.
Source§impl<K: Enumerable, V, const N: usize> EnumTable<K, V, N>
impl<K: Enumerable, V, const N: usize> EnumTable<K, V, N>
Sourcepub fn checked_from_pairs(
pairs: impl IntoIterator<Item = (K, V), IntoIter: ExactSizeIterator>,
) -> Option<Self>
pub fn checked_from_pairs( pairs: impl IntoIterator<Item = (K, V), IntoIter: ExactSizeIterator>, ) -> Option<Self>
Creates a new EnumTable from pairs, or returns None if pairs doesn’t
contain exactly one entry for each variant of K.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone, Debug, PartialEq)]
enum Color {
Red,
Green,
Blue,
}
let pairs = [
(Color::Red, "Red"),
(Color::Green, "Green"),
(Color::Blue, "Blue"),
];
let table = EnumTable::<Color, &str, { Color::COUNT }>::checked_from_pairs(pairs).unwrap();
assert_eq!(table.get(Color::Red), &"Red");
assert_eq!(table.get(Color::Green), &"Green");
assert_eq!(table.get(Color::Blue), &"Blue");A duplicate entry is rejected, instead of silently producing a table with an unrelated variant missing:
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone, Debug, PartialEq)]
enum Color {
Red,
Green,
Blue,
}
let pairs = [
(Color::Red, "Red"),
(Color::Green, "Green"),
(Color::Red, "Duplicate Red"),
];
assert_eq!(
EnumTable::<Color, &str, { Color::COUNT }>::checked_from_pairs(pairs),
None
);Source§impl<K: Enumerable, V, const N: usize> EnumTable<K, V, N>
impl<K: Enumerable, V, const N: usize> EnumTable<K, V, N>
Sourcepub fn from_fn(f: impl FnMut(K) -> V) -> Self
pub fn from_fn(f: impl FnMut(K) -> V) -> Self
Creates a new EnumTable by applying f to each variant of K.
Sourcepub fn try_from_fn<E>(f: impl FnMut(K) -> Result<V, E>) -> Result<Self, E>
pub fn try_from_fn<E>(f: impl FnMut(K) -> Result<V, E>) -> Result<Self, E>
Creates a new EnumTable by applying f to each variant of K,
stopping at the first Err.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone, Debug, PartialEq)]
enum Color {
Red,
Green,
Blue,
}
let result = EnumTable::<Color, &'static str, { Color::COUNT }>::try_from_fn(
|color| match color {
Color::Red => Ok("Red"),
Color::Green => Err("Failed to get value for Green"),
Color::Blue => Ok("Blue"),
},
);
assert_eq!(result, Err("Failed to get value for Green"));Sourcepub fn checked_from_fn(f: impl FnMut(K) -> Option<V>) -> Option<Self>
pub fn checked_from_fn(f: impl FnMut(K) -> Option<V>) -> Option<Self>
Creates a new EnumTable by applying f to each variant of K,
stopping at the first None.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone)]
enum Color {
Red,
Green,
Blue,
}
let table = EnumTable::<Color, &'static str, { Color::COUNT }>::checked_from_fn(
|color| match color {
Color::Red => Some("Red"),
Color::Green => None,
Color::Blue => Some("Blue"),
},
);
assert!(table.is_none());Sourcepub fn get_mut(&mut self, variant: K) -> &mut V
pub fn get_mut(&mut self, variant: K) -> &mut V
Returns a mutable reference to the value associated with variant.
Sourcepub fn set(&mut self, variant: K, value: V) -> V
pub fn set(&mut self, variant: K, value: V) -> V
Replaces the value associated with variant, returning the previous value.
Sourcepub const fn get_mut_const(&mut self, variant: K) -> &mut V
pub const fn get_mut_const(&mut self, variant: K) -> &mut V
const fn equivalent of Self::get_mut.
Sourcepub fn zip_with<U, W>(
self,
other: EnumTable<K, U, N>,
f: impl FnMut(K, V, U) -> W,
) -> EnumTable<K, W, N>
pub fn zip_with<U, W>( self, other: EnumTable<K, U, N>, f: impl FnMut(K, V, U) -> W, ) -> EnumTable<K, W, N>
Combines self and other into a new table by applying f to each variant
and its two values.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone)]
enum Stat {
Hp,
Attack,
Defense,
}
let base = EnumTable::<Stat, i32, { Stat::COUNT }>::from_fn(|s| match s {
Stat::Hp => 100,
Stat::Attack => 50,
Stat::Defense => 30,
});
let bonus = EnumTable::<Stat, i32, { Stat::COUNT }>::from_fn(|s| match s {
Stat::Hp => 20,
Stat::Attack => 10,
Stat::Defense => 5,
});
let total = base.zip_with(bonus, |_stat, a, b| a + b);
assert_eq!(total.get(Stat::Hp), &120);
assert_eq!(total.get(Stat::Attack), &60);
assert_eq!(total.get(Stat::Defense), &35);Sourcepub fn map<U>(self, f: impl FnMut(K, V) -> U) -> EnumTable<K, U, N>
pub fn map<U>(self, f: impl FnMut(K, V) -> U) -> EnumTable<K, U, N>
Consumes the table, returning a new one with each value transformed by f.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone)]
enum Size {
Small,
Medium,
Large,
}
let table = EnumTable::<Size, i32, { Size::COUNT }>::from_fn(|size| match size {
Size::Small => 1,
Size::Medium => 2,
Size::Large => 3,
});
let doubled = table.map(|_size, value| value * 2);
assert_eq!(doubled.get(Size::Small), &2);
assert_eq!(doubled.get(Size::Medium), &4);
assert_eq!(doubled.get(Size::Large), &6);Sourcepub fn for_each(&self, f: impl FnMut(K, &V))
pub fn for_each(&self, f: impl FnMut(K, &V))
Calls f with each variant and a reference to its value, in K::VARIANTS order.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone)]
enum Light {
Red,
Yellow,
Green,
}
let table = EnumTable::<Light, i32, { Light::COUNT }>::from_fn(|light| match light {
Light::Red => 1,
Light::Yellow => 2,
Light::Green => 3,
});
let mut sum = 0;
table.for_each(|_light, value| sum += value);
assert_eq!(sum, 6);Sourcepub fn for_each_mut(&mut self, f: impl FnMut(K, &mut V))
pub fn for_each_mut(&mut self, f: impl FnMut(K, &mut V))
Transforms each value in the table in place via f.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone)]
enum Level {
Low,
Medium,
High,
}
let mut table = EnumTable::<Level, i32, { Level::COUNT }>::from_fn(|level| match level {
Level::Low => 10,
Level::Medium => 20,
Level::High => 30,
});
table.for_each_mut(|_level, value| *value += 5);
assert_eq!(table.get(Level::Low), &15);
assert_eq!(table.get(Level::Medium), &25);
assert_eq!(table.get(Level::High), &35);Source§impl<K: Enumerable, V: Copy, const N: usize> EnumTable<K, V, N>
impl<K: Enumerable, V: Copy, const N: usize> EnumTable<K, V, N>
Sourcepub const fn from_elem(value: V) -> Self
pub const fn from_elem(value: V) -> Self
Creates a new EnumTable with value copied into every slot.
§Examples
use enum_table::{EnumTable, Enumerable};
#[derive(Enumerable, Copy, Clone)]
enum Status {
Active,
Inactive,
Pending,
}
let table = EnumTable::<Status, i32, { Status::COUNT }>::from_elem(42);
assert_eq!(table.get(Status::Active), &42);
assert_eq!(table.get(Status::Inactive), &42);
assert_eq!(table.get(Status::Pending), &42);Trait Implementations§
impl<K: Enumerable, V: Copy, const N: usize> Copy for EnumTable<K, V, N>
impl<K: Enumerable, V: Eq, const N: usize> Eq for EnumTable<K, V, N>
Source§impl<'a, K: Enumerable, V: Copy, const N: usize> Extend<(&'a K, &'a V)> for EnumTable<K, V, N>
impl<'a, K: Enumerable, V: Copy, const N: usize> Extend<(&'a K, &'a V)> for EnumTable<K, V, N>
Source§fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K: Enumerable, V, const N: usize> Extend<(K, V)> for EnumTable<K, V, N>
impl<K: Enumerable, V, const N: usize> Extend<(K, V)> for EnumTable<K, V, N>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)