[][src]Crate unique

Allocators which create one unique, shared pointer per distinct object. Useful for applications with highly-redundant or deeply nested data structures such as compilers, or automatic theorem provers.

If t1 == t2 (as determined by the allocator), then Id::new(t1) is pointer-equal to Id::new(t2). This property reduces memory use, reduces allocator hits, and allows for short-circuiting operations such as Eq and Hash by using the pointer rather than the data.

Example

use lazy_static::lazy_static;
use unique::{Allocated, Id, make_allocator};
use unique::allocators::HashAllocator;

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

make_allocator!(Expr, __EXPR_ALLOC, HashAllocator);

#[test]
fn example() {
    let two_x = Id::new(Expr::Const(2));
    let two_y = Id::new(Expr::Const(2));
    let three = Id::new(Expr::Const(3));

    assert_eq!(two_x, two_y);
    assert_ne!(two_x, three);
    assert_eq!(Expr::allocator().allocations(), 2);
}

Modules

allocators

Possible allocators to use

Macros

make_allocator

Structs

Id

A unique, shared pointer

Traits

Allocated

A type which has some allocator

Allocator

Allocate shared unique pointers