Crates.io | typed-arena |
lib.rs | typed-arena |
version | 2.0.2 |
source | src |
created_at | 2015-04-10 16:54:43.348496 |
updated_at | 2023-01-09 06:53:08.531421 |
description | The arena, a fast but limited type of allocator |
homepage | |
repository | https://github.com/SimonSapin/rust-typed-arena |
max_upload_size | |
id | 1833 |
size | 43,547 |
typed-arena
A fast (but limited) allocation arena for values of a single type.
Allocated objects are destroyed all at once, when the arena itself is destroyed. There is no deallocation of individual objects while the arena itself is still alive. The flipside is that allocation is fast: typically just a vector push.
There is also a method into_vec()
to recover ownership of allocated objects
when the arena is no longer required, instead of destroying everything.
use typed_arena::Arena;
struct Monster {
level: u32,
}
let monsters = Arena::new();
let goku = monsters.alloc(Monster { level: 9001 });
assert!(goku.level > 9000);
All allocated objects get the same lifetime, so you can safely create cycles between them. This can be useful for certain data structures, such as graphs and trees with parent pointers.
use std::cell::Cell;
use typed_arena::Arena;
struct CycleParticipant<'a> {
other: Cell<Option<&'a CycleParticipant<'a>>>,
}
let arena = Arena::new();
let a = arena.alloc(CycleParticipant { other: Cell::new(None) });
let b = arena.alloc(CycleParticipant { other: Cell::new(None) });
a.other.set(Some(b));
b.other.set(Some(a));
Use multiple arenas if you have only a couple different types or try
bumpalo
, which is a bump-allocation arena
can allocate heterogenous types of values.
Check out id-arena
or
generational-arena
.
Check out generational-arena
for an arena-style crate or look for a more traditional allocator.