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, Copy, Clone, Enumable)]
enum Test {
A,
B,
C,
}
const TABLE: EnumTable<Test, &'static str, { Test::COUNT }> = {
let mut builder = EnumTableBuilder::<Test, &'static str, { Test::COUNT }>::new();
unsafe {
builder.push_unchecked(&Test::A, "A");
builder.push_unchecked(&Test::B, "B");
builder.push_unchecked(&Test::C, "C");
builder.build_to_unchecked()
}
};
// 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 unsafe fn push_unchecked(&mut self, variant: &K, value: V)
pub const unsafe fn push_unchecked(&mut self, variant: &K, value: V)
Pushes a new element into the builder without safety checks.
§Safety
- The caller must ensure that elements are pushed in the correct order (sorted by discriminant).
- The caller must ensure that no variant is pushed more than once.
- The caller must ensure that the builder doesn’t exceed capacity N.
§Arguments
variant
- A reference to an enumeration variant.value
- The value to associate with the variant.
Sourcepub const unsafe fn build_unchecked(self) -> [(usize, V); N]
pub const unsafe fn build_unchecked(self) -> [(usize, V); N]
Builds the table from the pushed elements without checking if all variants are filled.
§Safety
The caller must ensure that all N variants have been pushed to the builder. If this is not the case, the resulting table will contain uninitialized memory.
§Returns
An array of tuples where each tuple contains a discriminant of an enumeration variant and its associated value.