Skip to main content

TermArena

Struct TermArena 

Source
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>

Source

pub const fn new() -> TermArena<CAP>

Creates an empty arena.

Source

pub fn push(&mut self, term: Term) -> Option<u32>

Push a term into the arena and return its index.

§Errors

Returns None if the arena is full.

Source

pub fn get(&self, index: u32) -> Option<&Term>

Returns a reference to the term at index, or None if out of bounds.

Source

pub const fn len(&self) -> u32

Returns the number of allocated nodes.

Source

pub const fn is_empty(&self) -> bool

Returns true if the arena has no allocated nodes.

Source

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.

Source

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).

Trait Implementations§

Source§

impl<const CAP: usize> Clone for TermArena<CAP>

Source§

fn clone(&self) -> TermArena<CAP>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const CAP: usize> Debug for TermArena<CAP>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<const CAP: usize> Default for TermArena<CAP>

Source§

fn default() -> TermArena<CAP>

Returns the “default value” for a type. Read more
Source§

impl<const CAP: usize> PartialEq for TermArena<CAP>

Source§

fn eq(&self, other: &TermArena<CAP>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const CAP: usize> Copy for TermArena<CAP>

Source§

impl<const CAP: usize> Eq for TermArena<CAP>

Source§

impl<const CAP: usize> StructuralPartialEq for TermArena<CAP>

Auto Trait Implementations§

§

impl<const CAP: usize> Freeze for TermArena<CAP>

§

impl<const CAP: usize> RefUnwindSafe for TermArena<CAP>

§

impl<const CAP: usize> Send for TermArena<CAP>

§

impl<const CAP: usize> Sync for TermArena<CAP>

§

impl<const CAP: usize> Unpin for TermArena<CAP>

§

impl<const CAP: usize> UnsafeUnpin for TermArena<CAP>

§

impl<const CAP: usize> UnwindSafe for TermArena<CAP>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.