pub struct HashInterner<T: ?Sized + Eq + Hash> { /* private fields */ }
Expand description
Interner for hashable values
The interner is cheaply cloneable by virtue of keeping the underlying storage
in an Arc
. Once the last
reference to this interner is dropped, it will clear its backing storage and
release all references to the interned values it has created that are still live.
Those values remain fully operational until dropped. Memory for the values
themselves is freed for each value individually once its last reference is dropped.
The interner’s ArcInner memory will only be deallocated once the last interned
value has been dropped (this is less than hundred bytes).
Implementations§
Source§impl<T: ?Sized + Eq + Hash> HashInterner<T>
impl<T: ?Sized + Eq + Hash> HashInterner<T>
pub fn new() -> Self
Sourcepub fn intern_ref(&self, value: &T) -> InternedHash<T>
pub fn intern_ref(&self, value: &T) -> InternedHash<T>
Intern a value from a shared reference by allocating new memory for it.
use intern_arc::{HashInterner, InternedHash};
let strings = HashInterner::<str>::new();
let i: InternedHash<str> = strings.intern_ref("hello world!");
Sourcepub fn intern_box(&self, value: Box<T>) -> InternedHash<T>
pub fn intern_box(&self, value: Box<T>) -> InternedHash<T>
Intern a value from an owned reference without allocating new memory for it.
use intern_arc::{HashInterner, InternedHash};
let strings = HashInterner::<str>::new();
let hello: Box<str> = "hello world!".into();
let i: InternedHash<str> = strings.intern_box(hello);
(This also works nicely with a String
that can be turned .into()
a Box
.)
Sourcepub fn intern_sized(&self, value: T) -> InternedHash<T>where
T: Sized,
pub fn intern_sized(&self, value: T) -> InternedHash<T>where
T: Sized,
Intern a sized value, allocating heap memory for it.
use intern_arc::{HashInterner, InternedHash};
let arrays = HashInterner::<[u8; 1000]>::new();
let i: InternedHash<[u8; 1000]> = arrays.intern_sized([0; 1000]);