pub struct AnyInternSet<S = FxBuildHasher> { /* private fields */ }Expand description
A type-erased interning set for storing and deduplicating values of a single type without interior mutability.
§Examples
use any_intern::AnyInternSet;
#[derive(PartialEq, Eq, Hash, Debug)]
struct A(u32);
let mut set = AnyInternSet::of::<A>();
unsafe {
let a1 = set.intern(A(42)).raw();
let a2 = set.intern(A(42)).raw();
assert_eq!(a1, a2); // Same value, same reference
let a3 = set.intern(A(99)).raw();
assert_ne!(a1, a3); // Different values, different references
}§Safety
Many methods in AnyInternSet are marked as unsafe because they rely on the caller to ensure
that the correct type is used when interacting with the interner. Using an incorrect type can
lead to undefined behavior.
Implementations§
Source§impl AnyInternSet
impl AnyInternSet
Source§impl<S: Default> AnyInternSet<S>
impl<S: Default> AnyInternSet<S>
pub fn default_of<K: 'static>() -> Self
Source§impl<S: BuildHasher> AnyInternSet<S>
impl<S: BuildHasher> AnyInternSet<S>
pub fn with_hasher<K: 'static>(hash_builder: S) -> Self
Sourcepub unsafe fn intern<K>(&mut self, value: K) -> Interned<'_, K>
pub unsafe fn intern<K>(&mut self, value: K) -> Interned<'_, K>
Stores a value in the set, returning a reference to the value.
This method inserts the given value into the set if it does not already exist. If the value already exists, a reference to the existing value is returned.
§Examples
use any_intern::AnyInternSet;
#[derive(PartialEq, Eq, Hash, Debug)]
struct A(u32);
let mut set = AnyInternSet::of::<A>();
unsafe {
let a1 = set.intern(A(42)).raw();
let a2 = set.intern(A(42)).raw();
assert_eq!(a1, a2); // Same value, same reference
}§Safety
Undefined behavior if incorrect type K is given.
Sourcepub unsafe fn intern_with<K, Q, F>(
&mut self,
key: &Q,
make_value: F,
) -> Interned<'_, K>
pub unsafe fn intern_with<K, Q, F>( &mut self, key: &Q, make_value: F, ) -> Interned<'_, K>
Stores a value in the set, creating it only if it does not already exist.
This method allows you to provide a key and a closure to generate the value. If the key already exists in the set, the closure is not called, and a reference to the existing value is returned. If the key does not exist, the closure is called to create the value, which is then stored in the set.
This method is useful when the value is expensive to compute, as it avoids unnecessary computation if the value already exists.
§Examples
use any_intern::AnyInternSet;
#[derive(PartialEq, Eq, Hash, Debug)]
struct A(i32);
impl std::borrow::Borrow<i32> for A {
fn borrow(&self) -> &i32 {
&self.0
}
}
let mut set = AnyInternSet::of::<A>();
unsafe {
let a = set.intern_with(&42, || A(42));
assert_eq!(*a, A(42));
assert_eq!(set.len(), 1);
let b = set.intern_with(&42, || A(99)); // Closure is not called
assert_eq!(*b, A(42));
assert_eq!(set.len(), 1);
let c = set.intern_with(&43, || A(43));
assert_eq!(*c, A(43));
assert_eq!(set.len(), 2);
}§Safety
Undefined behavior if incorrect type K is given.
Sourcepub unsafe fn get<K, Q>(&self, key: &Q) -> Option<Interned<'_, K>>
pub unsafe fn get<K, Q>(&self, key: &Q) -> Option<Interned<'_, K>>
Retrieves a reference to a value in the set based on the provided key.
This method checks if a value corresponding to the given key exists in the set. If it
exists, a reference to the value is returned. Otherwise, None is returned.
§Eaxmples
use any_intern::AnyInternSet;
let mut set = AnyInternSet::of::<i32>();
unsafe {
set.intern(42);
assert_eq!(set.get::<i32, _>(&42).as_deref(), Some(&42));
assert!(set.get::<i32, _>(&99).is_none());
}§Safety
Undefined behavior if incorrect type K is given.
Sourcepub fn is_type_of<K: 'static>(&self) -> bool
pub fn is_type_of<K: 'static>(&self) -> bool
Returns true if the set contains values of the given type.