Struct Interner

Source
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 to symbol type.
  • Interner::resolve: To resolve your already interned strings.
    • This maps from symbol type to string type.

Implementations§

Source§

impl<I: Intern + ?Sized, S: Symbol, H: BuildHasher + Default> Interner<I, S, H>

Source

pub fn new() -> Self

Creates a new empty Interner.

Source

pub fn with_capacity(cap: usize) -> Self

Creates a new StringInterner with the given initial capacity.

Source§

impl<I: Intern + ?Sized, S: Symbol, H: BuildHasher> Interner<I, S, H>

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

Source

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.

Source

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.

Source

pub fn shrink_to_fit(&mut self)

Shrink backend capacity to fit the interned strings exactly.

Source

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

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

Source

pub fn get_hash(&self, symbol: S) -> Option<u64>

Returns cached hash of the string for the given symbol.

Source

pub unsafe fn resolve_unchecked(&self, symbol: S) -> &I

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 Interner.

Source

pub unsafe fn get_hash_unchecked(&self, symbol: S) -> u64

Returns cached hash of 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 Interner.

Source

pub fn iter_with_hashes(&self) -> IterWithHashes<'_, I, S>

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

Source

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

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

Trait Implementations§

Source§

impl<I: Intern + ?Sized, S: Symbol, H: Clone> Clone for Interner<I, S, H>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<I: Intern + ?Sized, S, H> Debug for Interner<I, S, H>
where S: Debug + Symbol, H: BuildHasher,

Source§

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

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

impl<I: Intern + ?Sized, S: Symbol, H: BuildHasher + Default> Default for Interner<I, S, H>

Source§

fn default() -> Self

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

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

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<I: Intern + ?Sized, S: Symbol, H: BuildHasher + Default, T: AsRef<I>> FromIterator<T> for Interner<I, S, H>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, I: Intern + ?Sized, S: Symbol, H> IntoIterator for &'a Interner<I, S, H>

Source§

type Item = (S, &'a I)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, I, 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

Auto Trait Implementations§

§

impl<I, S, H> Freeze for Interner<I, S, H>
where H: Freeze, I: ?Sized,

§

impl<I, S, H> RefUnwindSafe for Interner<I, S, H>

§

impl<I, S, H> Send for Interner<I, S, H>
where H: Send, S: Send, <I as Intern>::Primitive: Send, I: ?Sized,

§

impl<I, S, H> Sync for Interner<I, S, H>
where H: Sync, S: Sync, <I as Intern>::Primitive: Sync, I: ?Sized,

§

impl<I, S, H> Unpin for Interner<I, S, H>
where H: Unpin, S: Unpin, <I as Intern>::Primitive: Unpin, I: ?Sized,

§

impl<I, S, H> UnwindSafe for Interner<I, S, H>
where H: UnwindSafe, S: UnwindSafe, <I as Intern>::Primitive: UnwindSafe, I: ?Sized,

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.