refcount_interner

Struct RcInterner

Source
pub struct RcInterner<T: ?Sized>(/* private fields */);
Expand description

An interner returning reference-counted pointers to the interned data

Interned objects will be deallocated when there are no references to them any more and shrink_to_fit() is called on the interner

§Example

use refcount_interner::RcInterner;

let mut interner = RcInterner::new();

let x = interner.intern(42);
let y = interner.intern(1337);

assert_eq!(*x, 42);
assert_ne!(x, y);
assert!(Rc::ptr_eq(&x, &interner.intern(42)));

Implementations§

Source§

impl<T: ?Sized + Hash + Eq> RcInterner<T>

Source

pub fn new() -> RcInterner<T>

Create a new, empty interner.

§Example
let mut interner = RcInterner::new();
Source

pub fn try_intern(&self, t: &T) -> Option<Rc<T>>

Attempt to get a reference to an already interned object.

If the object has already been interned, an option containing a reference to the already interned object will be returned.

If the object has not yet been interned, None will be returned.

§Example
let mut interner = RcInterner::new();

let x = interner.intern(42);
assert_eq!(interner.try_intern(&42), Some(Rc::new(42)));
assert_eq!(interner.try_intern(&1337), None);
Source

pub fn intern_boxed(&mut self, t: Box<T>) -> Rc<T>

Intern a boxed object

This method must be used to intern unsized types, since unsized types cannot be passed to intern(). The two most common unsized types, &[T] and &str can be interned with intern_slice() and intern_str() as well.

If the object has already been interned, the passed object will be dropped and deallocated, and a reference to the already interned object will be returned.

If the object has not yet been interned, the passed object will be moved into an Rc<T>, remembered for future calls to intern(), and returned.

§Example
let mut interner = RcInterner::new();

let x = Box::new(42);
let y = interner.intern_boxed(x);

assert_eq!(*y, 42);
Source

pub fn shrink_to_fit(&mut self)

Deallocate all interned objects that are no longer referenced and shrink the internal storage to fit.

§Example
let mut interner = RcInterner::new();

let x = interner.intern(42);
let y = interner.intern(1337);
let z = y.clone();

drop(x);
drop(y);

interner.shrink_to_fit();
assert_eq!(interner.try_intern(&42), None);
assert_eq!(interner.try_intern(&1337), Some(Rc::new(1337)));
Source§

impl<T: Sized + Hash + Eq> RcInterner<T>

Source

pub fn intern(&mut self, t: T) -> Rc<T>

Intern an owned object

If the object has already been interned, the passed object will be dropped, and a reference to the already interned object will be returned.

If the object has not yet been interned, the passed object will be moved into an Rc<T>, remembered for future calls to intern(), and returned.

§Example
let mut interner = RcInterner::new();

let x = interner.intern(42);
let y = interner.intern(1337);

assert_eq!(*x, 42);
assert_ne!(x, y);
assert!(Rc::ptr_eq(&x, &interner.intern(42)));
Source§

impl<T: Sized + Hash + Eq + Clone> RcInterner<T>

Source

pub fn intern_cloned(&mut self, t: &T) -> Rc<T>

Intern a borrowed object, cloning if it has not yet been interned

If the object has already been interned, a reference to the already interned object will be returned.

If the object has not yet been interned, the passed object will be moved into an Rc<T>, remembered for future calls to intern(), and returned.

§Example
let mut interner = RcInterner::new();

let x = 42;
let y = interner.intern_cloned(&x);

assert_eq!(x, *y);
Source§

impl<T: Sized + Hash + Eq + Clone> RcInterner<[T]>

Source

pub fn intern_slice(&mut self, t: &[T]) -> Rc<[T]>

Intern a slice object

This method can be used to intern slices without boxing them.

If the slice has already been interned, a reference to the already interned slice will be returned.

If the slice has not yet been interned, the passed object will be cloned into an Rc<[T]>, remembered for future calls to intern(), and returned.

§Example
let mut interner = RcInterner::new();

let x = interner.intern_slice(&[1, 2, 3]);

assert_eq!(x.as_ref(), &[1, 2, 3]);
Source

pub fn intern_vec(&mut self, t: Vec<T>) -> Rc<[T]>

Intern an owned vector

If the slice behind the vector has already been interned, a reference to the already / interned slice will be returned.

If the slice has not yet been interned, the passed vector will be moved into an Rc<[T]>, remembered for future calls to intern(), and returned.

§Example

let mut interner = RcInterner::new();

let v = vec![1, 2, 3];
let x = interner.intern_vec(v);

assert_eq!(x.as_ref(), &[1, 2, 3]);
Source§

impl RcInterner<str>

Source

pub fn intern_str(&mut self, t: &str) -> Rc<str>

Intern a string slice

This method can be used to intern string slices without boxing them.

If the string slice has already been interned, a reference to the already interned string slice will be returned.

If the string slice has not yet been interned, the passed object will be cloned into an Rc<str>, remembered for future calls to intern(), and returned.

§Example
let mut interner = RcInterner::new();

let x = interner.intern_str("hello");

assert_eq!(x.as_ref(), "hello");
Source

pub fn intern_string(&mut self, t: String) -> Rc<str>

Intern an owned string

If the string has already been interned, a reference to the already interned string slice will be returned.

If the string has not yet been interned, the passed string will be moved into an Rc<str>, remembered for future calls to intern(), and returned.

§Example

let mut interner = RcInterner::new();

let s = String::from("hello");
let x = interner.intern_string(s);

assert_eq!(x.as_ref(), "hello");

Trait Implementations§

Source§

impl<T: Debug + ?Sized> Debug for RcInterner<T>

Source§

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

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

impl<T: ?Sized> Default for RcInterner<T>

Source§

fn default() -> RcInterner<T>

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

Auto Trait Implementations§

§

impl<T> Freeze for RcInterner<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for RcInterner<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> !Send for RcInterner<T>

§

impl<T> !Sync for RcInterner<T>

§

impl<T> Unpin for RcInterner<T>
where T: ?Sized,

§

impl<T> UnwindSafe for RcInterner<T>
where T: RefUnwindSafe + ?Sized,

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.