pub struct Interner<I: Intern + ?Sized, S: Symbol = DefaultSymbol, H = DefaultHashBuilder> { /* private fields */ }
Expand description
Data structure to intern and resolve strings.
Caches strings efficiently, with minimal memory footprint and associates them with unique symbols. These symbols allow constant time comparisons and look-ups to the underlying interned strings.
The following API covers the main functionality:
Interner::intern
: To intern a new string.- This maps from
string
type tosymbol
type.
- This maps from
Interner::resolve
: To resolve your already interned strings.- This maps from
symbol
type tostring
type.
- This maps from
Implementations§
Source§impl<I: Intern + ?Sized, S: Symbol, H: BuildHasher> Interner<I, S, H>
impl<I: Intern + ?Sized, S: Symbol, H: BuildHasher> Interner<I, S, H>
Sourcepub fn with_hasher(hash_builder: H) -> Self
pub fn with_hasher(hash_builder: H) -> Self
Creates a new empty StringInterner
with the given hasher.
Sourcepub fn with_capacity_and_hasher(cap: usize, hash_builder: H) -> Self
pub fn with_capacity_and_hasher(cap: usize, hash_builder: H) -> Self
Creates a new empty StringInterner
with the given initial capacity and the given hasher.
Sourcepub fn get<T>(&self, string: T) -> Option<S>where
T: AsRef<I>,
pub fn get<T>(&self, string: T) -> Option<S>where
T: AsRef<I>,
Returns the symbol for the given string if any.
Can be used to query if a string has already been interned without interning.
Sourcepub fn intern_and_hash<T: AsRef<I>>(&mut self, string: T) -> (S, u64)
pub fn intern_and_hash<T: AsRef<I>>(&mut self, string: T) -> (S, u64)
Interns the given string.
Returns a symbol for resolution into the original string, and its hash.
§Panics
If the interner already interns the maximum number of strings possible by the chosen symbol type.
Sourcepub fn intern<T: AsRef<I>>(&mut self, string: T) -> S
pub fn intern<T: AsRef<I>>(&mut self, string: T) -> S
Interns the given string.
Returns a symbol for resolution into the original string.
§Panics
If the interner already interns the maximum number of strings possible by the chosen symbol type.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink backend capacity to fit the interned strings exactly.
Sourcepub fn resolve(&self, symbol: S) -> Option<&I>
pub fn resolve(&self, symbol: S) -> Option<&I>
Returns the string for the given `symbol`` if any.
Sourcepub fn get_hash(&self, symbol: S) -> Option<u64>
pub fn get_hash(&self, symbol: S) -> Option<u64>
Returns cached hash of the string for the given symbol
.
Sourcepub unsafe fn resolve_unchecked(&self, symbol: S) -> &I
pub unsafe fn resolve_unchecked(&self, symbol: S) -> &I
Sourcepub unsafe fn get_hash_unchecked(&self, symbol: S) -> u64
pub unsafe fn get_hash_unchecked(&self, symbol: S) -> u64
Sourcepub fn iter_with_hashes(&self) -> IterWithHashes<'_, I, S> ⓘ
pub fn iter_with_hashes(&self) -> IterWithHashes<'_, I, S> ⓘ
Returns an iterator that yields all interned strings, their symbols, and hashes.
Trait Implementations§
Source§impl<I: Intern + ?Sized, S: Symbol, H: BuildHasher + Default, T: AsRef<I>> Extend<T> for Interner<I, S, H>
impl<I: Intern + ?Sized, S: Symbol, H: BuildHasher + Default, T: AsRef<I>> Extend<T> for Interner<I, S, H>
Source§fn extend<It>(&mut self, iter: It)where
It: IntoIterator<Item = T>,
fn extend<It>(&mut self, iter: It)where
It: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)