1 unstable release

Uses old Rust 2015

0.1.0 Apr 10, 2022

#340 in Memory management

Download history 205/week @ 2024-02-25 196/week @ 2024-03-03 151/week @ 2024-03-10 114/week @ 2024-03-17 184/week @ 2024-03-24 198/week @ 2024-03-31 123/week @ 2024-04-07 220/week @ 2024-04-14 235/week @ 2024-04-21 392/week @ 2024-04-28 411/week @ 2024-05-05 416/week @ 2024-05-12 400/week @ 2024-05-19 324/week @ 2024-05-26 165/week @ 2024-06-02 209/week @ 2024-06-09

1,121 downloads per month
Used in wpf-gpu-raster

MIT license

37KB
570 lines

typed-arena-nomut

This is a fork of the typed-arena arena crate that returns an immutable reference instead of mutable one. This allows iteration on the arena items while they're borrowed.

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.

Example

use typed_arena_nomut::Arena;

struct Monster {
    level: u32,
}

let monsters = Arena::new();

let goku = monsters.alloc(Monster { level: 9001 });
assert!(goku.level > 9000);

Safe Cycles

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_nomut::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));

Alternatives

Need to allocate many different types of values?

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.

Want allocation to return identifiers instead of references and dealing with references and lifetimes everywhere?

Check out id-arena or generational-arena.

Need to deallocate individual objects at a time?

Check out generational-arena for an arena-style crate or look for a more traditional allocator.

No runtime deps