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

#[macro_use] extern crate lazy_static;
#[macro_use] extern crate unique;

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

#[derive(PartialEq, Eq, Hash)]
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(value: Self) -> Uniq<Self> {
        EXPR_BACKING.unique(value)
    }
}

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

Modules

Data structures for implementing backing stores.

Macros

Shorthand for Uniq::new.

Structs

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

Traits

A type which has some backing store.