3 [![](https://img.shields.io/crates/v/id-arena.svg)](https://crates.io/crates/id-arena)
4 [![](https://img.shields.io/crates/d/id-arena.svg)](https://crates.io/crates/id-arena)
5 [![Travis CI Build Status](https://travis-ci.org/fitzgen/id-arena.svg?branch=master)](https://travis-ci.org/fitzgen/id-arena)
7 A simple, id-based arena.
11 Allocate objects and get an identifier for that object back, *not* a
12 reference to the allocated object. Given an id, you can get a shared or
13 exclusive reference to the allocated object from the arena. This id-based
14 approach is useful for constructing mutable graph data structures.
16 If you want allocation to return a reference, consider [the `typed-arena`
17 crate](https://github.com/SimonSapin/rust-typed-arena/) instead.
21 This arena does not support deletion, which makes its implementation simple
22 and allocation fast. If you want deletion, you need a way to solve the ABA
23 problem. Consider using [the `generational-arena`
24 crate](https://github.com/fitzgen/generational-arena) instead.
28 This crate's arenas can only contain objects of a single type `T`. If you
29 need an arena of objects with heterogeneous types, consider another crate.
31 ### `#![no_std]` Support
33 Requires the `alloc` nightly feature. Disable the on-by-default `"std"` feature:
36 [dependencies.id-arena]
38 default-features = false
43 If the `rayon` feature of this crate is activated:
47 id-arena = { version = "2", features = ["rayon"] }
50 then you can use [`rayon`](https://crates.io/crates/rayon)'s support for
51 parallel iteration. The `Arena` type will have a `par_iter` family of
52 methods where appropriate.
57 use id_arena::{Arena, Id};
59 type AstNodeId = Id<AstNode>;
61 #[derive(Debug, Eq, PartialEq)]
83 let mut ast_nodes = Arena::<AstNode>::new();
85 // Create the AST for `a * (b + 3)`.
86 let three = ast_nodes.alloc(AstNode::Const(3));
87 let b = ast_nodes.alloc(AstNode::Var("b".into()));
88 let b_plus_three = ast_nodes.alloc(AstNode::Add {
92 let a = ast_nodes.alloc(AstNode::Var("a".into()));
93 let a_times_b_plus_three = ast_nodes.alloc(AstNode::Mul {
98 // Can use indexing to access allocated nodes.
99 assert_eq!(ast_nodes[three], AstNode::Const(3));