pub struct DroplessInterner<S = FxBuildHasher> { /* 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<S: BuildHasher> DroplessInterner<S>
impl<S: BuildHasher> DroplessInterner<S>
pub fn with_hasher(hash_builder: S) -> Self
Sourcepub fn intern<K: Dropless + ?Sized>(&self, value: &K) -> Interned<'_, K>
pub fn intern<K: Dropless + ?Sized>(&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 intern_formatted_str<K: Display + ?Sized>(
&self,
value: &K,
upper_size: usize,
) -> Result<Interned<'_, str>, Error>
pub fn intern_formatted_str<K: Display + ?Sized>( &self, value: &K, upper_size: usize, ) -> Result<Interned<'_, str>, Error>
Stores a value in the interner as a formatted string through Display, returning a
reference to the interned value.
This method provides a buffer for making string. This will be benefit in terms of
performance when you frequently make String via something like to_string() by exploiting
chunk memory.
This method first formats the given value using the Display trait and stores the resulting
string in the interner’s buffer, then compares the string with existing values. If the
formatted string already exists in the interner, formatted string is discarded and reference
to the existing value is returned.
If you give insufficient upper_size, then error is returned.
§Examples
use any_intern::DroplessInterner;
let interner = DroplessInterner::new();
let value = 42;
let interned = interner.intern_formatted_str(&value, 10).unwrap();
assert_eq!(&*interned, "42");Sourcepub fn get<K: Dropless + ?Sized>(&self, value: &K) -> Option<Interned<'_, K>>
pub fn get<K: Dropless + ?Sized>(&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").as_deref(), Some("hello"));
assert!(interner.get("world").is_none());