pub struct Id<T> { /* private fields */ }Expand description
A small, copyable, type-tagged handle to one value in an Arena.
An Id<T> is the stable way to refer to an allocated value: it stays valid for
the life of the arena that issued it, and it is a single u32 — four bytes,
the same as a bare index — so passing one is no more expensive than passing an
integer. Unlike a &T, it carries no borrow, so a tree of nodes can store ids
pointing at one another without tangling the borrow checker — which is exactly
why an AST or IR holds child handles rather than child references.
The T tag is compile-time only (it occupies no space): it stops an
Id<Expr> from being passed where an Id<Stmt> is expected, so a handle can
only ever index the arena that produced it. Id<T> is Copy, Eq, Ord, and
Hash for every T — the tag never adds a bound — so it works as a map key
regardless of what it points at.
Resolve a handle with Arena::get /
get_mut; there is no public constructor, so an Id
can only come from an Arena::alloc.
§Examples
use arena_lang::Arena;
let mut arena = Arena::new();
let id = arena.alloc("node");
// The handle round-trips back to the value it named.
assert_eq!(arena.get(id), Some(&"node"));
// It is Copy and four bytes wide, whatever it points at.
let also = id;
assert_eq!(id, also);
assert_eq!(core::mem::size_of_val(&id), 4);