Crate unique

source ·
Expand description

Allows the creation of pointers which are guaranteed to be equal pointers for equal data.

Useful for applications with highly-redundant or deeply nested data structures such as compilers or automatic theorem provers.

In future this crate may support recovery of allocated memory that is no longer required (via e.g. mark-and-sweep), but for now all backing stores leak memory as required.

Example

use unique::{Backed, Uniq};
use unique::backing::HashBacking;

enum Expr {
    Const(i32),
    Add(Uniq<Expr>, Uniq<Expr>),
}

lazy_static! {
    static ref EXPR_BACKING: HashBacking<Expr> = HashBacking::new(100);
}

impl Backed for Expr {
    fn unique(self) -> Uniq<Self> {
        EXPR_BACKING.unique(self)
    }
}

fn example() {
    let two_x = Uniq::new(Const(2));
    let two_y = Uniq::new(Const(2));
    assert!(two_x.as_ref() as *const Expr == two_y.as_ref() as *const Expr);
}

Modules

Data structures for implementing backing stores.

Structs

A unique pointer to data, allocated by a backing store.

Traits

A type which has some backing store.