Struct Interner

Source
pub struct Interner<S = RandomState> { /* private fields */ }
Expand description

An interner will keep track of strings and ensure there is only one allocation for any given string contents.

For example:

let interner = Interner::new();
let foo0 = interner.intern(String::from("foo"));
let foo1 = interner.intern(String::from("foo"));
assert!(InternedStr::ptr_eq(&foo0, &foo1));

Because foo0 and foo1 have the same contents, they become a single allocation.

Interned strings are immutable, which means that you must construct the finished string before interning it.

This is useful if you have many instances of the same strings (e.g., if 200 different structs contain the string "foo", an interner allows there to be 200 pointers to one allocation, rather than 200 different allocations).

This Interner is thread-safe, meaning that it implements both Send and Sync (when S implements Send, which the default does).

Implementations§

Source§

impl Interner

Source

pub fn new() -> Self

Constructs a new Interner.

Source§

impl<S> Interner<S>

Source

pub fn with_hasher(hasher: S) -> Self

Constructs a new Interner with the given hasher. See BuildHasher for more information.

Source

pub fn from_set(strings: HashSet<InternedStr, S>) -> Self

Construct a new Interner with the given set’s contents already interned. The new Interner will also use the given set’s hasher.

Source

pub fn into_set(self) -> HashSet<InternedStr, S>

Consume this Interner and return a set containing all of strings that were interned. The returned set also uses the same hasher.

§Panics

This method panics if this Interner has been poisoned.

Source

pub fn clear(&self)

Locks this Interner and removes all of the interned strings, or blocks until it is able to do so.

interner.clear() is equivalent to intenerer.lock().clear(). (See LockedInterner::clear.)

§Panics

This method panics if this Interner has been poisoned, and it may panic if this Interner is already locked on this thread.

Source

pub fn lock(&self) -> LockedInterner<'_, S>

Locks this Interner on the current thread until the returned LockedInterner is dropped, or blocks until it is able to do so.

While it is locked, the current thread has exclusive access to this Interner’s methods (accessible from the LockedInterner; any methods used directly on self may panic). This enables some additional functionality, most notably LockedInterner::iter.

If a panic occurs on the current thread while this Interner is locked, it will become poisoned.

§Panics

This method panics if this Interner has been poisoned, and it may panic if this Interner is already locked on this thread.

Source§

impl<S: BuildHasher> Interner<S>

Source

pub fn intern(&self, string: impl AsRef<str>) -> InternedStr
where S: BuildHasher,

Locks this Interner, saves the given string if it is not already saved, and returns a reference to the saved allocation, or blocks until it is able to do so.

interner.intern(string) is equivalent to interner.lock().intern(string). (See LockedInterner::intern.)

§Panics

This method panics if this Interner has been poisoned, and it may panic if this Interner is already locked on this thread.

Source

pub fn contains(&self, string: impl AsRef<str>) -> bool

Returns whether the given string has already been saved, or blocks until it is able to do so.

Source

pub fn get(&self, string: impl AsRef<str>) -> Option<InternedStr>

If the given string has already been saved, returns a reference to the saved allocation, or None otherwise, or blocks until it is able to do so.

Trait Implementations§

Source§

impl<S: Clone> Clone for Interner<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S> Debug for Interner<S>

Source§

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

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

impl<S: Default> Default for Interner<S>

Source§

fn default() -> Self

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

impl<A, S> FromIterator<A> for Interner<S>

Source§

fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<S> IntoIterator for Interner<S>

Source§

type Item = Arc<str>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

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

fn into_iter(self) -> IntoIter

Creates an iterator from a value. Read more
Source§

impl<S: BuildHasher> PartialEq for Interner<S>

Source§

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

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

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

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

impl<S: BuildHasher> Eq for Interner<S>

Auto Trait Implementations§

§

impl<S = RandomState> !Freeze for Interner<S>

§

impl<S> RefUnwindSafe for Interner<S>

§

impl<S> Send for Interner<S>
where S: Send,

§

impl<S> Sync for Interner<S>
where S: Send,

§

impl<S> Unpin for Interner<S>
where S: Unpin,

§

impl<S> UnwindSafe for Interner<S>

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.