pub struct DroplessInterner<S = BuildHasherDefault<FxHasher>> { /* private fields */ }Expand description
An interner for storing and deduplicating values without requiring ownership.
The DroplessInterner is designed for interning values that implement the Dropless trait.
It allows efficient storage and retrieval of values by ensuring that each unique value is stored
only once. Interning with this type always copies the given value into an internal buffer,
making it suitable for use cases where ownership is not required.
§Examples
use any_intern::DroplessInterner;
let mut interner = DroplessInterner::new();
// Interning strings
let hello = interner.intern("hello");
let world = interner.intern("world");
let another_hello = interner.intern("hello");
assert_eq!(hello, another_hello); // Same value, same reference
assert_ne!(hello, world); // Different values, different references
// Checking if a value exists
assert!(interner.get("hello").is_some());
assert!(interner.get("unknown").is_none());
// Clearing the interner
interner.clear();
assert!(interner.is_empty());§Safety
The DroplessInterner relies on the Dropless trait for converting values to and from raw
byte representations. It is the responsibility of the Dropless implementation to ensure
memory safety and alignment when interacting with the interner.
Implementations§
Source§impl DroplessInterner
impl DroplessInterner
pub fn new() -> DroplessInterner
Source§impl<S> DroplessInterner<S>where
S: BuildHasher,
impl<S> DroplessInterner<S>where
S: BuildHasher,
pub fn with_hasher(hash_builder: S) -> DroplessInterner<S>
Sourcepub fn intern<K>(&self, value: &K) -> Interned<'_, K>
pub fn intern<K>(&self, value: &K) -> Interned<'_, K>
Stores a value in the interner, returning a reference to the interned value.
This method inserts the given value into the interner if it does not already exist. If the value already exists, a reference to the existing value is returned. The value is copied into an internal buffer, making it suitable for use cases where ownership is not required.
§Examples
use any_intern::DroplessInterner;
let interner = DroplessInterner::new();
// Interning strings
let hello = interner.intern("hello");
let world = interner.intern("world");
let another_hello = interner.intern("hello");
assert_eq!(hello, another_hello); // Same value, same reference
assert_ne!(hello, world); // Different values, different references
// Interning arrays
let array1 = interner.intern(&[1, 2, 3]);
let array2 = interner.intern(&[1, 2, 3]);
let array3 = interner.intern(&[4, 5, 6]);
assert_eq!(array1, array2); // Same value, same reference
assert_ne!(array1, array3); // Different values, different referencesSourcepub fn get<K>(&self, value: &K) -> Option<Interned<'_, K>>
pub fn get<K>(&self, value: &K) -> Option<Interned<'_, K>>
Retrieves a reference to a value in the interner based on the provided key.
This method checks if a value corresponding to the given key exists in the interner. If it
exists, a reference to the interned value is returned. Otherwise, None is returned.
§Eaxmples
use any_intern::DroplessInterner;
let interner = DroplessInterner::new();
// Interning strings
let hello = interner.intern("hello");
assert_eq!(*interner.get("hello").unwrap(), "hello");
assert!(interner.get("world").is_none());