pub struct StringInterner { /* private fields */ }Expand description
Thread-safe string interner using Box<str> arena for single-allocation storage.
Strings are stored in an append-only Vec<Box<str>> (pointer-stable on heap).
A hash-keyed map provides O(1) lookup with collision chaining.
Send and Sync are derived from RwLock — no manual unsafe impl needed.
Implementations§
Source§impl StringInterner
impl StringInterner
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new string interner with the given capacity
Sourcepub fn intern(&self, s: &str) -> InternedString
pub fn intern(&self, s: &str) -> InternedString
Intern a string, returning its handle.
Fast path: read lock to check existence via hash.
Slow path: write lock, double-check, allocate Box<str>.
Sourcepub fn resolve(&self, id: InternedString) -> Option<&str>
pub fn resolve(&self, id: InternedString) -> Option<&str>
Resolve an interned string back to &str.
§Safety argument
We return a &str whose lifetime is tied to &self (the interner),
not to the read-lock guard. This is safe because:
- Strings are never removed (append-only vec).
Box<str>is heap-allocated and pointer-stable —pushto the vec does not move existing boxes.- The interner outlives all returned references (borrow checker enforces this).
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for StringInterner
impl !RefUnwindSafe for StringInterner
impl Send for StringInterner
impl Sync for StringInterner
impl Unpin for StringInterner
impl UnsafeUnpin for StringInterner
impl UnwindSafe for StringInterner
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more