pub struct StringTable<O = u32, I = u16, const NULL_PADDED: bool = false, A: Allocator + Clone = Global>where
O: Offset,
I: StringIndex,{ /* private fields */ }Expand description
Immutable string storage.
All strings are stored in a single contiguous UTF-8 byte buffer.
An offset table maps each StringId to a byte range.
The table is immutable once built. This keeps references returned by Self::get
valid for the lifetime of &self and avoids mutation-related reallocation issues.
The offset table always contains one extra value at the end (a sentinel)
equal to bytes.len(). This allows get to resolve a range with two
offset reads.
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.
§Example
use lite_strtab::StringTableBuilder;
let mut builder = StringTableBuilder::new();
let a = builder.try_push("cat").unwrap();
let b = builder.try_push("dog").unwrap();
let table = builder.build();
assert_eq!(table.get(a), Some("cat"));
assert_eq!(table.get(b), Some("dog"));Implementations§
Source§impl<O: Offset, I: StringIndex, const NULL_PADDED: bool, A: Allocator + Clone> StringTable<O, I, NULL_PADDED, A>
impl<O: Offset, I: StringIndex, const NULL_PADDED: bool, A: Allocator + Clone> StringTable<O, I, NULL_PADDED, A>
Sourcepub unsafe fn get_unchecked(&self, id: StringId<I>) -> &str
pub unsafe fn get_unchecked(&self, id: StringId<I>) -> &str
Returns the string for a given ID without bounds checks.
§Safety
id must be in bounds (id < self.len()).
Sourcepub fn iter(&self) -> StringTableIter<'_, O, NULL_PADDED> ⓘ
pub fn iter(&self) -> StringTableIter<'_, O, NULL_PADDED> ⓘ
Returns an iterator over all strings.