Crates.io | simple_arena |
lib.rs | simple_arena |
version | 0.2.0 |
source | src |
created_at | 2023-11-28 13:32:54.967788 |
updated_at | 2023-11-28 13:32:54.967788 |
description | A simple arena allocator for Rust |
homepage | |
repository | https://gitlab.com/bolli24/simple_arena |
max_upload_size | |
id | 1051999 |
size | 8,508 |
simple-arena
A simple but fast arena allocator for a single type. Using it allows you to allocate structs that are guaranteed to have the same lifetime. This is useful for data structures that need to be able to cyclically reference each other, like trees. The downside is that you can't deallocate individual entries, you can deallocate the whole arena at once.
use simple_arena::Arena;
fn main() {
let mut arena = Arena::new();
let node1 = arena.alloc(1);
let node2 = arena.alloc(2);
assert_eq!(*node1, 1);
assert_eq!(*node2, 2);
}
The arena can also be used to allocate structs that contain references to other structs in the arena using Cell
.
use simple_arena::Arena;
use std::cell::Cell;
struct Node<'a> {
next: Cell<Option<&'a Node<'a>>>,
value: u32,
}
fn main() {
let mut arena = Arena::new();
let node1 = arena.alloc(Node { next: Cell::new(None), value: 1 });
let node2 = arena.alloc(Node { next: Cell::new(Some(node1)), value: 2 });
node1.next.set(Some(node2));
assert_eq!(node1.next.get().unwrap().value, 2);
}
Do you need some more features like iterators: typed-arena
Do you need to allocate multiple types: bumpalo