Skip to main content

DroplessInterner

Struct DroplessInterner 

Source
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 DroplessInterner

Source

pub fn new() -> Self

Source§

impl<S: BuildHasher> DroplessInterner<S>

Source

pub fn with_hasher(hash_builder: S) -> Self

Source

pub fn len(&self) -> usize

Returns number of values the interner contains.

Source

pub fn is_empty(&self) -> bool

Returns true if the interner is empty.

Source

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 references
Source

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");
Source

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());
Source

pub fn clear(&mut self)

Removes all items in the interner.

Although the interner support interior mutability, clear method requires mutable access to the interner to invalidate all Interneds referencing the interner.

Trait Implementations§

Source§

impl<S: Debug> Debug for DroplessInterner<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Default> Default for DroplessInterner<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.