Debian Codegraph @

README.md

raw symbols SCIP
1 # `typed-arena`
2
3 [![](https://docs.rs/typed-arena/badge.svg)](https://docs.rs/typed-arena/)
4 [![](https://img.shields.io/crates/v/typed-arena.svg)](https://crates.io/crates/typed-arena)
5 [![](https://img.shields.io/crates/d/typed-arena.svg)](https://crates.io/crates/typed-arena)
6 [![Github Actions Build Status](https://github.com/SimonSapin/rust-typed-arena/workflows/CI/badge.svg)](https://github.com/SimonSapin/rust-typed-arena/actions)
7
8 **A fast (but limited) allocation arena for values of a single type.**
9
10 Allocated objects are destroyed all at once, when the arena itself is destroyed.
11 There is no deallocation of individual objects while the arena itself is still
12 alive. The flipside is that allocation is fast: typically just a vector push.
13
14 There is also a method `into_vec()` to recover ownership of allocated objects
15 when the arena is no longer required, instead of destroying everything.
16
17 ## Example
18
19 ```rust
20 use typed_arena::Arena;
21
22 struct Monster {
23 level: u32,
24 }
25
26 let monsters = Arena::new();
27
28 let goku = monsters.alloc(Monster { level: 9001 });
29 assert!(goku.level > 9000);
30 ```
31
32 ## Safe Cycles
33
34 All allocated objects get the same lifetime, so you can safely create cycles
35 between them. This can be useful for certain data structures, such as graphs
36 and trees with parent pointers.
37
38 ```rust
39 use std::cell::Cell;
40 use typed_arena::Arena;
41
42 struct CycleParticipant<'a> {
43 other: Cell<Option<&'a CycleParticipant<'a>>>,
44 }
45
46 let arena = Arena::new();
47
48 let a = arena.alloc(CycleParticipant { other: Cell::new(None) });
49 let b = arena.alloc(CycleParticipant { other: Cell::new(None) });
50
51 a.other.set(Some(b));
52 b.other.set(Some(a));
53 ```
54
55 ## Alternatives
56
57 ### Need to allocate many different types of values?
58
59 Use multiple arenas if you have only a couple different types or try
60 [`bumpalo`](https://crates.io/crates/bumpalo), which is a bump-allocation arena
61 can allocate heterogenous types of values.
62
63 ### Want allocation to return identifiers instead of references and dealing with references and lifetimes everywhere?
64
65 Check out [`id-arena`](https://crates.io/crates/id-arena) or
66 [`generational-arena`](https://crates.io/crates/generational-arena).
67
68 ### Need to deallocate individual objects at a time?
69
70 Check out [`generational-arena`](https://crates.io/crates/generational-arena)
71 for an arena-style crate or look for a more traditional allocator.