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<S> Interner<S>
impl<S> Interner<S>
Sourcepub fn with_hasher(hasher: S) -> Self
pub fn with_hasher(hasher: S) -> Self
Constructs a new Interner with the given hasher. See BuildHasher for more information.
Sourcepub fn from_set(strings: HashSet<InternedStr, S>) -> Self
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.
Sourcepub fn into_set(self) -> HashSet<InternedStr, S>
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.