id-arena

Travis CI Build Status

A simple, id-based arena.

Id-based

Allocate objects and get an identifier for that object back, not a reference to the allocated object. Given an id, you can get a shared or exclusive reference to the allocated object from the arena. This id-based approach is useful for constructing mutable graph data structures.

If you want allocation to return a reference, consider the typed-arena crate instead.

No Deletion

This arena does not support deletion, which makes its implementation simple and allocation fast. If you want deletion, you need a way to solve the ABA problem. Consider using the generational-arena crate instead.

Homogeneous

This crate's arenas can only contain objects of a single type T. If you need an arena of objects with heterogeneous types, consider another crate.

#![no_std] Support

Requires the alloc nightly feature. Disable the on-by-default "std" feature:

toml [dependencies.id-arena] version = "2" default-features = false

rayon Support

If the rayon feature of this crate is activated:

toml [dependencies] id-arena = { version = "2", features = ["rayon"] }

then you can use rayon's support for parallel iteration. The Arena type will have a par_iter family of methods where appropriate.

Example

```rust use id_arena::{Arena, Id};

type AstNodeId = Id;

[derive(Debug, Eq, PartialEq)]

pub enum AstNode { Const(i64), Var(String), Add { lhs: AstNodeId, rhs: AstNodeId, }, Sub { lhs: AstNodeId, rhs: AstNodeId, }, Mul { lhs: AstNodeId, rhs: AstNodeId, }, Div { lhs: AstNodeId, rhs: AstNodeId, }, }

let mut ast_nodes = Arena::::new();

// Create the AST for a * (b + 3). let three = astnodes.alloc(AstNode::Const(3)); let b = astnodes.alloc(AstNode::Var("b".into())); let bplusthree = astnodes.alloc(AstNode::Add { lhs: b, rhs: three, }); let a = astnodes.alloc(AstNode::Var("a".into())); let atimesbplusthree = astnodes.alloc(AstNode::Mul { lhs: a, rhs: bplus_three, });

// Can use indexing to access allocated nodes. asserteq!(astnodes[three], AstNode::Const(3)); ```