Skip to main content

Crate safe_bump

Crate safe_bump 

Source
Expand description

Safe bump-pointer arena allocator.

safe-bump provides a typed arena allocator built entirely with safe Rust (zero unsafe blocks). Values are allocated by appending to an internal buffer and accessed via stable Idx<T> indices.

§Key properties

  • Zero unsafe: enforced by #![forbid(unsafe_code)]
  • Auto Drop: destructors run on reset, rollback, and arena drop
  • Checkpoint/rollback: save state and discard speculative allocations
  • O(1) amortized allocation: backed by Vec::push
  • O(1) index access: direct indexing into backing storage

§Example

use safe_bump::{Arena, Idx};

let mut arena: Arena<String> = Arena::new();
let a: Idx<String> = arena.alloc(String::from("hello"));
let b: Idx<String> = arena.alloc(String::from("world"));

assert_eq!(arena[a], "hello");
assert_eq!(arena[b], "world");
assert_eq!(arena.len(), 2);

// Checkpoint and rollback
let cp = arena.checkpoint();
let _tmp = arena.alloc(String::from("temporary"));
assert_eq!(arena.len(), 3);

arena.rollback(cp); // "temporary" is dropped
assert_eq!(arena.len(), 2);

§References

  • Hanson, 1990 — “Fast Allocation and Deallocation of Memory Based on Object Lifetimes”

Structs§

Arena
Typed arena allocator.
Checkpoint
Saved allocation state for Arena::rollback.
Idx
Stable index into an Arena<T>.
IterIndexed
Iterator yielding (Idx<T>, &T) pairs in allocation order.
IterIndexedMut
Mutable iterator yielding (Idx<T>, &mut T) pairs in allocation order.