Struct EnumTableBuilder

Source
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>

Source

pub const fn new() -> Self

Creates a new EnumTableBuilder with an uninitialized table.

§Returns

A new instance of EnumTableBuilder.

Source

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.
Source

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.

Source

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.

Source

pub const fn len(&self) -> usize

Returns the number of elements the builder is expected to hold.

§Returns

The number of elements N.

Source

pub const fn is_empty(&self) -> bool

Returns false as the builder is expected to be filled completely.

§Returns

Always returns false.

Trait Implementations§

Source§

impl<K: Enumable, V, const N: usize> Default for EnumTableBuilder<K, V, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V, const N: usize> Freeze for EnumTableBuilder<K, V, N>
where V: Freeze,

§

impl<K, V, const N: usize> RefUnwindSafe for EnumTableBuilder<K, V, N>

§

impl<K, V, const N: usize> Send for EnumTableBuilder<K, V, N>
where K: Send, V: Send,

§

impl<K, V, const N: usize> Sync for EnumTableBuilder<K, V, N>
where K: Sync, V: Sync,

§

impl<K, V, const N: usize> Unpin for EnumTableBuilder<K, V, N>
where K: Unpin, V: Unpin,

§

impl<K, V, const N: usize> UnwindSafe for EnumTableBuilder<K, V, N>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.