EnumTableBuilder

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

Source

pub const fn new() -> Self

Creates a new EnumTableBuilder with an uninitialized table.

§Returns

A new instance of EnumTableBuilder.

Source

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

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.

Source

pub const unsafe fn build_to_unchecked(self) -> EnumTable<K, V, N>

Builds the EnumTable 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.

§Returns

An EnumTable containing the elements pushed into the builder.

Source

pub const fn len(&self) -> usize

Returns the number of elements pushed into the builder.

Source

pub const fn capacity(&self) -> usize

Returns the capacity of the builder.

Source

pub const fn is_empty(&self) -> bool

Returns true if the builder has no elements pushed yet.

§Returns

true if no elements have been pushed, false otherwise.

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.