pub struct TermArena<const CAP: usize> { /* private fields */ }Expand description
Stack-resident arena for Term trees.
Fixed capacity determined by the const generic CAP.
All Term child references are u32 indices into this arena.
#![no_std]-safe: no heap allocation.
§Examples
use uor_foundation::enforcement::{TermArena, Term, TermList};
use uor_foundation::{WittLevel, PrimitiveOp};
// Build the expression `add(3, 5)` bottom-up in an arena.
let mut arena = TermArena::<4>::new();
// Push leaves first:
let idx_3 = arena.push(uor_foundation::pipeline::literal_u64(3, WittLevel::W8));
let idx_5 = arena.push(uor_foundation::pipeline::literal_u64(5, WittLevel::W8));
// Push the application node, referencing the leaves by index:
let idx_add = arena.push(Term::Application {
operator: PrimitiveOp::Add,
args: TermList { start: idx_3.unwrap_or(0), len: 2 },
});
assert_eq!(arena.len(), 3);
// Retrieve a node by index:
let node = arena.get(idx_add.unwrap_or(0));
assert!(node.is_some());Implementations§
Source§impl<const CAP: usize> TermArena<CAP>
impl<const CAP: usize> TermArena<CAP>
Sourcepub fn get(&self, index: u32) -> Option<&Term>
pub fn get(&self, index: u32) -> Option<&Term>
Returns a reference to the term at index, or None if out of bounds.
Sourcepub fn as_slice(&self) -> &[Option<Term>]
pub fn as_slice(&self) -> &[Option<Term>]
v0.2.2 T4.5.b: returns the populated prefix of the arena as a slice.
Each entry is Some(term) for the populated indices 0..len();
downstream consumers index into this slice via TermList::start and
TermList::len to walk the children of an Application/Match node.
Sourcepub const fn from_slice(slice: &'static [Term]) -> TermArena<CAP>
pub const fn from_slice(slice: &'static [Term]) -> TermArena<CAP>
Wiki ADR-022 D2: const constructor that copies a static term slice
into the arena. Used by the prism_model! proc-macro to emit a
const ROUTE: TermArena<CAP> = TermArena::from_slice(ROUTE_SLICE)
declaration alongside the model — the const ROUTE_SLICE carrying
the term tree, this from_slice wrapping it into the arena form
crate::pipeline::run_route consumes.
CAP MUST be at least slice.len(); if the slice exceeds the
arena’s capacity the trailing terms are silently dropped (the
prism_model! macro emits an arena sized to fit the route’s term
count plus headroom, so this case is unreachable from the macro’s
output).