pub struct StringInterner<B, H = DefaultHashBuilder>where
B: Backend,{ /* 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:
StringInterner::get_or_intern
: To intern a new string.- This maps from
string
type tosymbol
type.
- This maps from
StringInterner::resolve
: To resolve your already interned strings.- This maps from
symbol
type tostring
type.
- This maps from
Implementations§
Source§impl<B, H> StringInterner<B, H>
impl<B, H> StringInterner<B, H>
Sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Creates a new StringInterner
with the given initial capacity.
Source§impl<B, H> StringInterner<B, H>
impl<B, H> StringInterner<B, 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<<B as Backend>::Symbol>
pub fn get<T>(&self, string: T) -> Option<<B as Backend>::Symbol>
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 get_or_intern<T>(&mut self, string: T) -> <B as Backend>::Symbol
pub fn get_or_intern<T>(&mut self, string: T) -> <B as Backend>::Symbol
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 get_or_intern_static(
&mut self,
string: &'static str,
) -> <B as Backend>::Symbol
pub fn get_or_intern_static( &mut self, string: &'static str, ) -> <B as Backend>::Symbol
Interns the given 'static
string.
Returns a symbol for resolution into the original string.
§Note
This is more efficient than StringInterner::get_or_intern
since it might
avoid some memory allocations if the backends supports this.
§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: <B as Backend>::Symbol) -> Option<&str>
pub fn resolve(&self, symbol: <B as Backend>::Symbol) -> Option<&str>
Returns the string for the given `symbol`` if any.
Sourcepub unsafe fn resolve_unchecked(&self, symbol: <B as Backend>::Symbol) -> &str
pub unsafe fn resolve_unchecked(&self, symbol: <B as Backend>::Symbol) -> &str
Returns the string for the given symbol
without performing any checks.
§Safety
It is the caller’s responsibility to provide this method with symbol
s
that are valid for the StringInterner
.
Trait Implementations§
Source§impl<B, H> Clone for StringInterner<B, H>
impl<B, H> Clone for StringInterner<B, H>
Source§impl<B, H> Debug for StringInterner<B, H>
impl<B, H> Debug for StringInterner<B, H>
Source§impl Default for StringInterner<DefaultBackend>
impl Default for StringInterner<DefaultBackend>
Source§impl<'de, B, H> Deserialize<'de> for StringInterner<B, H>
impl<'de, B, H> Deserialize<'de> for StringInterner<B, H>
Source§fn deserialize<D>(deserializer: D) -> Result<StringInterner<B, H>, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<StringInterner<B, H>, D::Error>where
D: Deserializer<'de>,
Source§impl<B, H, T> Extend<T> for StringInterner<B, H>
impl<B, H, T> Extend<T> for StringInterner<B, H>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: 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
)Source§impl<B, H, T> FromIterator<T> for StringInterner<B, H>
impl<B, H, T> FromIterator<T> for StringInterner<B, H>
Source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
Source§impl<'a, B, H> IntoIterator for &'a StringInterner<B, H>where
B: Backend,
<B as Backend>::Symbol: Symbol,
&'a B: IntoIterator<Item = (<B as Backend>::Symbol, &'a str)>,
H: BuildHasher,
impl<'a, B, H> IntoIterator for &'a StringInterner<B, H>where
B: Backend,
<B as Backend>::Symbol: Symbol,
&'a B: IntoIterator<Item = (<B as Backend>::Symbol, &'a str)>,
H: BuildHasher,
Source§impl<B, H> PartialEq for StringInterner<B, H>
impl<B, H> PartialEq for StringInterner<B, H>
Source§impl<B, H> Serialize for StringInterner<B, H>where
B: Backend,
<B as Backend>::Symbol: Symbol,
for<'a> &'a B: IntoIterator<Item = (<B as Backend>::Symbol, &'a str)>,
H: BuildHasher,
impl<B, H> Serialize for StringInterner<B, H>where
B: Backend,
<B as Backend>::Symbol: Symbol,
for<'a> &'a B: IntoIterator<Item = (<B as Backend>::Symbol, &'a str)>,
H: BuildHasher,
impl<B, H> Eq for StringInterner<B, H>
Auto Trait Implementations§
impl<B, H> Freeze for StringInterner<B, H>
impl<B, H> RefUnwindSafe for StringInterner<B, H>
impl<B, H> Send for StringInterner<B, H>
impl<B, H> Sync for StringInterner<B, H>
impl<B, H> Unpin for StringInterner<B, H>
impl<B, H> UnwindSafe for StringInterner<B, H>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)