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:
Ois the byte-offset type (seeOffset). It bounds total UTF-8 bytes and costssize_of::<O>()per string inside thecrate::StringTable.Iis the string-ID type (seeStringIndex) used bycrate::StringId. It limits string count and costssize_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>
impl StringTableBuilder<u32, u16, false, Global>
Sourcepub fn with_capacity(strings: usize, bytes: usize) -> Self
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>
impl StringTableBuilder<u32, u16, true, Global>
Sourcepub fn new_null_padded() -> Self
pub fn new_null_padded() -> Self
Creates an empty builder with null-padded mode using the global allocator.
Sourcepub fn with_capacity_null_padded(strings: usize, bytes: usize) -> Self
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>
impl<O: Offset, I: StringIndex, const NULL_PADDED: bool, A: Allocator + Clone> StringTableBuilder<O, I, NULL_PADDED, A>
Sourcepub fn with_capacity_in(strings: usize, bytes: usize, allocator: A) -> Self
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.
Sourcepub fn try_push(&mut self, value: &str) -> Result<StringId<I>>
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.
Sourcepub fn build(self) -> StringTable<O, I, NULL_PADDED, A>
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.