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 mut 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 not thread-safe (which is to say, it is implements neither Send nor Sync). For a thread-safe variant, see the sync module.

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.

Source

pub fn clear(&mut self)

Removes all of the interned strings.

Source

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

An iterator over all of the currently interned strings.

Source§

impl<S: BuildHasher> Interner<S>

Source

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

Saves the given string if it is not already saved, and returns a reference the saved allocation.

Source

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

Returns whether the given string has already been saved.

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.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a copy 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<'a, S> IntoIterator for &'a Interner<S>

Source§

type Item = &'a Rc<str>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

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

fn into_iter(self) -> Iter<'a>

Creates an iterator from a value. Read more
Source§

impl<S> IntoIterator for Interner<S>

Source§

type Item = Rc<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> Freeze for Interner<S>
where S: Freeze,

§

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

§

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

§

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

§

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

§

impl<S> UnwindSafe for Interner<S>
where S: 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.