[][src]Crate unique

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 lazy_static::lazy_static;
use unique::{Backed, Id};
use unique::backing::HashBacking;

#[derive(PartialEq, Eq, Hash)]
enum Expr {
    Const(i32),
    Add(Id<Expr>, Id<Expr>),
}

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

impl Backed for Expr {
    fn unique(value: Self) -> Id<Self> {
        EXPR_BACKING.unique(value)
    }
}

#[test]
fn example() {
    let two_x = Id::new(Expr::Const(2));
    let two_y = Id::new(Expr::Const(2));
    assert!(two_x.as_ref() as *const Expr == two_y.as_ref() as *const Expr);
}

Modules

backing

Data structures for implementing backing stores.

Structs

Id

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

Traits

Backed

A type which has some backing store.