pub struct EnumTableBuilder<K: Enumable, V, const N: usize> { /* private fields */ }
Expand description
A builder for creating an EnumTable
with a specified number of elements.
EnumTableBuilder
allows for the incremental construction of an EnumTable
by pushing elements one by one and then building the final table.
§Note
The builder is expected to be filled completely before building the table.
If the builder is not filled completely, the build
and build_to
method will panic.
For a clearer and more concise approach, consider using the crate::et
macro.
§Example
use enum_table::{EnumTable, Enumable, builder::EnumTableBuilder,};
#[derive(Debug)]
enum Test {
A,
B,
C,
}
impl Enumable for Test {
const VARIANTS: &'static [Self] = &[Test::A, Test::B, Test::C];
}
const TABLE: EnumTable<Test, &'static str, { Test::COUNT }> = {
let mut builder = EnumTableBuilder::<Test, &'static str, { Test::COUNT }>::new();
builder.push(&Test::A, "A");
builder.push(&Test::B, "B");
builder.push(&Test::C, "C");
builder.build_to()
};
// Access values associated with enum variants
assert_eq!(TABLE.get(&Test::A), &"A");
assert_eq!(TABLE.get(&Test::B), &"B");
assert_eq!(TABLE.get(&Test::C), &"C");
Implementations§
Source§impl<K: Enumable, V, const N: usize> EnumTableBuilder<K, V, N>
impl<K: Enumable, V, const N: usize> EnumTableBuilder<K, V, N>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new EnumTableBuilder
with an uninitialized table.
§Returns
A new instance of EnumTableBuilder
.
Sourcepub const fn push(&mut self, variant: &K, value: V)
pub const fn push(&mut self, variant: &K, value: V)
Pushes a new element into the builder.
§Arguments
variant
- A reference to an enumeration variant.value
- The value to associate with the variant.
Sourcepub const fn build(self) -> [(usize, V); N]
pub const fn build(self) -> [(usize, V); N]
Builds the table from the pushed elements.
§Returns
An array of tuples where each tuple contains a discriminant of an enumeration variant and its associated value.
Sourcepub const fn build_to(self) -> EnumTable<K, V, N>
pub const fn build_to(self) -> EnumTable<K, V, N>
Builds the EnumTable
from the pushed elements.
§Returns
An EnumTable
containing the elements pushed into the builder.