Skip to main content

StringTableBuilder

Struct StringTableBuilder 

Source
pub struct StringTableBuilder<O = u32, I = u16, const NULL_PADDED: bool = false, A: Allocator + Clone = Global>
where O: Offset, I: StringIndex,
{ /* private fields */ }
Expand description

Incremental builder for crate::StringTable.

Each call to Self::try_push appends string bytes to a single byte buffer and appends one offset.

Generic parameters control capacity and metadata size:

  • O is the byte-offset type (see Offset). It bounds total UTF-8 bytes and costs size_of::<O>() per string inside the crate::StringTable.
  • I is the string-ID type (see StringIndex) used by crate::StringId. It limits string count and costs size_of::<I>() per stored ID field (table index) in your own structs.

The common choice is O = u32, I = u16: meaning 4 GiB of UTF-8 data and 64Ki entries per table.

This is 4 bytes per string offset in the table and 2 bytes per StringID (index into table) inside your structs. For comparison: Box<str> == 16 bytes, String == 24 bytes.

By default, inserted strings are not NUL-terminated. Set NULL_PADDED = true to store strings with a trailing NUL byte.

Implementations§

Source§

impl StringTableBuilder<u32, u16, false, Global>

Source

pub fn new() -> Self

Creates an empty builder using the global allocator.

Source

pub fn with_capacity(strings: usize, bytes: usize) -> Self

Creates a builder with reserved capacities using the global allocator.

strings is the expected number of strings, bytes is the expected total number of UTF-8 bytes.

Source§

impl StringTableBuilder<u32, u16, true, Global>

Source

pub fn new_null_padded() -> Self

Creates an empty builder with null-padded mode using the global allocator.

Source

pub fn with_capacity_null_padded(strings: usize, bytes: usize) -> Self

Creates a null-padded builder with reserved capacities using the global allocator.

strings is the expected number of strings, bytes is the expected total number of UTF-8 bytes.

Source§

impl<O: Offset, I: StringIndex, const NULL_PADDED: bool, A: Allocator + Clone> StringTableBuilder<O, I, NULL_PADDED, A>

Source

pub fn new_in(allocator: A) -> Self

Creates an empty builder with a custom allocator.

Source

pub fn with_capacity_in(strings: usize, bytes: usize, allocator: A) -> Self

Creates a builder with reserved capacities and a custom allocator.

strings is the expected number of strings, bytes is the expected total number of UTF-8 bytes.

Source

pub fn len(&self) -> usize

Number of strings currently pushed.

Source

pub fn is_empty(&self) -> bool

Returns true when the builder has no strings.

Source

pub fn bytes_len(&self) -> usize

Current total byte length of pushed string data.

Source

pub fn try_push(&mut self, value: &str) -> Result<StringId<I>>

Appends a string and returns its StringId.

Returns an error when total string count exceeds the configured ID type, or when the byte length cannot be represented by the configured offset type.

Source

pub fn build(self) -> StringTable<O, I, NULL_PADDED, A>

Finalizes into an immutable crate::StringTable.

This does not copy string bytes. Internal vectors are converted into boxed slices so the resulting table is immutable and compact.

Trait Implementations§

Source§

impl Default for StringTableBuilder<u32, u16, false, Global>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<O, I, const NULL_PADDED: bool, A> Freeze for StringTableBuilder<O, I, NULL_PADDED, A>
where A: Freeze,

§

impl<O, I, const NULL_PADDED: bool, A> RefUnwindSafe for StringTableBuilder<O, I, NULL_PADDED, A>

§

impl<O, I, const NULL_PADDED: bool, A> Send for StringTableBuilder<O, I, NULL_PADDED, A>
where A: Send,

§

impl<O, I, const NULL_PADDED: bool, A> Sync for StringTableBuilder<O, I, NULL_PADDED, A>
where A: Sync,

§

impl<O, I, const NULL_PADDED: bool, A> Unpin for StringTableBuilder<O, I, NULL_PADDED, A>
where I: Unpin, A: Unpin,

§

impl<O, I, const NULL_PADDED: bool, A> UnsafeUnpin for StringTableBuilder<O, I, NULL_PADDED, A>
where A: UnsafeUnpin,

§

impl<O, I, const NULL_PADDED: bool, A> UnwindSafe for StringTableBuilder<O, I, NULL_PADDED, A>

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.