alloc-once
Allocate several values of different types in a single bump allocation, and get back a guard that owns the memory and frees it (running destructors) when it drops.
use alloc_once;
let mut guard = alloc_once!;
// Deref to the tuple, then destructure into independent &mut references.
let = &mut *guard;
*count += 100;
bytes = 99;
assert_eq!;
assert_eq!;
assert_eq!;
// `guard` drops here: it runs each value's destructor, then frees the arena.
No arena to thread through your code, no bumpalo in your dependency
list — alloc_once! hides all of it.
How it works
alloc_once!(a, b, c) packs the values into one tuple (a, b, c), creates a bumpalo arena sized
to exactly the bytes that tuple needs (Bump::with_capacity(size_of_val(&tuple))), allocates
the tuple in a single bump, and returns an AllocOnce<T> guard.
- One allocation, exact size. The tuple layout is fixed at compile time, so the arena reserves a single chunk of precisely the needed bytes.
- A guard that owns the arena.
AllocOnce<T>derefs to the tuple (&*guard/&mut *guard). When it drops it runs the value's destructor and then frees the arena — unlike a rawBump::alloc, whose values are never dropped. - bumpalo stays hidden. You depend only on
alloc-once; the macro expands to paths inside this crate, so your code andCargo.tomlnever mention bumpalo.
Caveats
- Exact capacity is a request. bumpalo may round the underlying chunk up slightly for its own bookkeeping; the guarantee is that the bundle fits in the first chunk with no second allocation.
- Lifetime is the guard's. All references borrow from the guard, so the guard must outlive them (the borrow checker enforces this).
License
Licensed under either of MIT or Apache-2.0 at your option.