Struct StringInterner

Source
pub struct StringInterner<S, H = RandomState>
where S: Symbol, H: BuildHasher,
{ /* private fields */ }
Expand description

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.

Implementations§

Source§

impl<S> StringInterner<S>
where S: Symbol,

Source

pub fn new() -> StringInterner<S, RandomState>

Creates a new empty StringInterner.

Source

pub fn with_capacity(cap: usize) -> Self

Creates a new StringInterner with the given initial capacity.

Source

pub fn capacity(&self) -> usize

Returns the number of elements the StringInterner can hold without reallocating.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be interned into self.

The collection may reserve more space to avoid frequent allocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

Source§

impl<S, H> StringInterner<S, H>
where S: Symbol, H: BuildHasher,

Source

pub fn with_hasher(hash_builder: H) -> StringInterner<S, H>

Creates a new empty StringInterner with the given hasher.

Source

pub fn with_capacity_and_hasher( cap: usize, hash_builder: H, ) -> StringInterner<S, H>

Creates a new empty StringInterner with the given initial capacity and the given hasher.

Source

pub fn get_or_intern<T>(&mut self, val: T) -> S
where T: Into<String> + AsRef<str>,

Interns the given value.

Returns a symbol to access it within this interner.

This either copies the contents of the string (e.g. for str) or moves them into this interner (e.g. for String).

Source

pub fn resolve(&self, symbol: S) -> Option<&Arc<str>>

Returns the string slice associated with the given symbol if available, otherwise returns None.

Source

pub unsafe fn resolve_unchecked(&self, symbol: S) -> &Arc<str>

Returns the string associated with the given symbol.

§Note

This does not check whether the given symbol has an associated string for the given string interner instance.

§Safety

This will result in undefined behaviour if the given symbol had no associated string for this interner instance.

Source

pub fn get<T>(&self, val: T) -> Option<S>
where T: AsRef<str>,

Returns the symbol associated with the given string for this interner if existent, otherwise returns None.

Source

pub fn len(&self) -> usize

Returns the number of uniquely interned strings within this interner.

Source

pub fn is_empty(&self) -> bool

Returns true if the string interner holds no elements.

Source

pub fn iter(&self) -> Iter<'_, S>

Returns an iterator over the interned strings.

Source

pub fn iter_values(&self) -> Values<'_, S>

Returns an iterator over all intern indices and their associated strings.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the interner as much as possible.

Trait Implementations§

Source§

impl<S, H> Clone for StringInterner<S, H>
where S: Symbol, H: Clone + BuildHasher,

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<S, H> Debug for StringInterner<S, H>
where S: Symbol + Debug, H: BuildHasher + Debug,

Source§

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

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

impl Default for StringInterner<Sym, RandomState>

Source§

fn default() -> Self

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

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

Source§

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

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

impl<T, S> Extend<T> for StringInterner<S>
where S: Symbol, T: Into<String> + 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<T, S> FromIterator<T> for StringInterner<S>
where S: Symbol, T: Into<String> + 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<S, H> IntoIterator for StringInterner<S, H>
where S: Symbol, H: BuildHasher,

Source§

type Item = (S, Arc<str>)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<S>

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<S, H> PartialEq for StringInterner<S, H>
where S: Symbol, H: BuildHasher,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Sym, H> Serialize for StringInterner<Sym, H>
where Sym: Symbol, H: BuildHasher,

Source§

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

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

impl<S, H> Eq for StringInterner<S, H>
where S: Symbol + Eq, H: BuildHasher + Eq,

Source§

impl<S, H> Send for StringInterner<S, H>
where S: Symbol + Send, H: BuildHasher,

Source§

impl<S, H> Sync for StringInterner<S, H>
where S: Symbol + Sync, H: BuildHasher,

Auto Trait Implementations§

§

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

§

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

§

impl<S, H> Unpin for StringInterner<S, H>
where H: Unpin, S: Unpin,

§

impl<S, H> UnwindSafe for StringInterner<S, H>
where S: UnwindSafe, H: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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

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