Struct string_interner::StringInterner

source ·
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:

Implementations§

source§

impl<B, H> StringInterner<B, H>
where B: Backend, <B as Backend>::Symbol: Symbol, H: BuildHasher + Default,

source

pub fn new() -> Self

Creates a new empty StringInterner.

source

pub fn with_capacity(cap: usize) -> Self

Creates a new StringInterner with the given initial capacity.

source§

impl<B, H> StringInterner<B, H>
where B: Backend, <B as Backend>::Symbol: Symbol, H: BuildHasher,

source

pub fn with_hasher(hash_builder: H) -> Self

Creates a new empty StringInterner with the given hasher.

source

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.

source

pub fn len(&self) -> usize

Returns the number of strings interned by the interner.

source

pub fn is_empty(&self) -> bool

Returns true if the string interner has no interned strings.

source

pub fn get<T>(&self, string: T) -> Option<<B as Backend>::Symbol>
where T: AsRef<str>,

Returns the symbol for the given string if any.

Can be used to query if a string has already been interned without interning.

source

pub fn get_or_intern<T>(&mut self, string: T) -> <B as Backend>::Symbol
where T: AsRef<str>,

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.

source

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.

source

pub fn shrink_to_fit(&mut self)

Shrink backend capacity to fit the interned strings exactly.

source

pub fn resolve(&self, symbol: <B as Backend>::Symbol) -> Option<&str>

Returns the string for the given `symbol`` if any.

source

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 symbols that are valid for the StringInterner.

source

pub fn iter(&self) -> <B as Backend>::Iter<'_>

Returns an iterator that yields all interned strings and their symbols.

Trait Implementations§

source§

impl<B, H> Clone for StringInterner<B, H>
where B: Backend + Clone, <B as Backend>::Symbol: Symbol, H: BuildHasher + Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<B, H> Debug for StringInterner<B, H>
where B: Backend + Debug, <B as Backend>::Symbol: Symbol + Debug, H: BuildHasher,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for StringInterner<DefaultBackend>

source§

fn default() -> Self

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

impl<'de, B, H> Deserialize<'de> for StringInterner<B, H>
where B: Backend, <B as Backend>::Symbol: Symbol, H: BuildHasher + Default,

source§

fn deserialize<D>(deserializer: D) -> Result<StringInterner<B, H>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<B, H, T> Extend<T> for StringInterner<B, H>
where B: Backend, <B as Backend>::Symbol: Symbol, H: BuildHasher, T: AsRef<str>,

source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = T>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<B, H, T> FromIterator<T> for StringInterner<B, H>
where B: Backend, <B as Backend>::Symbol: Symbol, H: BuildHasher + Default, T: AsRef<str>,

source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
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,

§

type Item = (<B as Backend>::Symbol, &'a str)

The type of the elements being iterated over.
§

type IntoIter = <&'a B as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<B, H> PartialEq for StringInterner<B, H>
where B: Backend + PartialEq, <B as Backend>::Symbol: Symbol, H: BuildHasher,

source§

fn eq(&self, rhs: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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,

source§

fn serialize<T>(&self, serializer: T) -> Result<T::Ok, T::Error>
where T: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<B, H> Eq for StringInterner<B, H>
where B: Backend + Eq, <B as Backend>::Symbol: Symbol, H: BuildHasher,

Auto Trait Implementations§

§

impl<B, H> Freeze for StringInterner<B, H>
where H: Freeze, B: Freeze,

§

impl<B, H> RefUnwindSafe for StringInterner<B, H>

§

impl<B, H> Send for StringInterner<B, H>
where H: Send, B: Send, <B as Backend>::Symbol: Send,

§

impl<B, H> Sync for StringInterner<B, H>
where H: Sync, B: Sync, <B as Backend>::Symbol: Sync,

§

impl<B, H> Unpin for StringInterner<B, H>
where H: Unpin, B: Unpin, <B as Backend>::Symbol: Unpin,

§

impl<B, H> UnwindSafe for StringInterner<B, H>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,