Skip to main content

StringTable

Struct StringTable 

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

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

§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 StringTable<u32, u16, false, Global>

Source

pub fn empty() -> Self

Creates an empty table using the global allocator.

Source§

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

Source

pub fn empty_in(allocator: A) -> Self

Creates an empty table with a custom allocator.

Source

pub fn len(&self) -> usize

Number of strings in the table.

Source

pub fn is_empty(&self) -> bool

Returns true when the table has no strings.

Source

pub fn get(&self, id: StringId<I>) -> Option<&str>

Returns the string for a given ID.

Source

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()).

Source

pub fn iter(&self) -> StringTableIter<'_, O, NULL_PADDED>

Returns an iterator over all strings.

Source

pub fn as_bytes(&self) -> &[u8]

Returns the contiguous byte storage.

Source

pub fn contains(&self, value: &str) -> bool

Returns true if any stored string equals value.

Source

pub fn offsets(&self) -> &[O]

Returns the offset table, including the final sentinel.

Source

pub fn byte_range(&self, id: StringId<I>) -> Option<Range<usize>>

Returns the byte range for a given ID.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<O, I, const NULL_PADDED: bool, A> UnwindSafe for StringTable<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.