[][src]Struct refcount_interner::RcInterner

pub struct RcInterner<T: ?Sized>(_);

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

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

pub fn new() -> RcInterner<T>[src]

Create a new, empty interner.

Example

let mut interner = RcInterner::new();

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

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

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

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

pub fn shrink_to_fit(&mut self)[src]

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

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

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

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

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

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

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

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

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

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]);

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

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]);

impl RcInterner<str>[src]

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

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

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

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

impl<T: Debug + ?Sized> Debug for RcInterner<T>[src]

impl<T: ?Sized> Default for RcInterner<T>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for RcInterner<T>

impl<T> !Send for RcInterner<T>

impl<T> !Sync for RcInterner<T>

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

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.