Skip to main content

InternPool

Struct InternPool 

Source
pub struct InternPool { /* private fields */ }
Expand description

The raw storage for interned strings.

  • strings is append-only; indices are stable for the lifetime of the pool.
  • map maps each string’s content to its index for O(1) deduplication.
  • static_ptrs caches the &'static str obtained by leaking each string exactly once, so subsequent resolve calls are O(1) slice lookups.

Implementations§

Source§

impl InternPool

Source

pub fn new() -> Self

Creates a new, empty InternPool.

Source

pub fn intern_str(&mut self, s: &str) -> u32

Interns s, returning a stable index.

If s is already present the existing index is returned without any allocation. Otherwise a new String is pushed and indexed.

Source

pub fn resolve_str(&mut self, id: u32) -> Option<&'static str>

Resolves index id to a &'static str.

The string is leaked into a Box<str> on the first call for id and the resulting pointer is cached. Subsequent calls for the same id return the cached pointer without any allocation.

Returns None if id is out of range.

Source

pub fn len(&self) -> usize

Returns the number of distinct strings stored.

Source

pub fn is_empty(&self) -> bool

Returns true when no strings have been interned.

Source

pub fn total_bytes(&self) -> usize

Returns the total byte count of all interned strings.

Source

pub fn stats(&self) -> InternPoolStats

Returns a InternPoolStats snapshot.

Trait Implementations§

Source§

impl Default for InternPool

Source§

fn default() -> Self

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

Auto Trait Implementations§

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.