Term

Struct Term 

Source
pub struct Term(/* private fields */);
Expand description

A compact, copyable handle referencing a term stored in a Arena.

Internally a Term stores a single [Handle] enum variant. On 64‑bit targets the discriminant and associated payload occupy 16 bytes in total. Users should never construct Term values directly; instead use the associated constructors or the convenience macros in the [term] module. Instances of Term are cheap to copy (Copy and Clone).

Implementations§

Source§

impl Term

Source

pub fn display<'a>(&'a self, arena: &'a Arena) -> TermDisplay<'a>

Return a TermDisplay suitable for formatting with fmt::Display.

Use this method when you want to render a term:

println!("{}", term.display(&arena));
Source§

impl Term

Source

pub const UNIT: Self

Constant representing the zero‑arity tuple (unit). Internally this is the atom "unit" encoded as a small atom. It may be copied freely and does not depend on any arena.

Source

pub const NIL: Self

Constant representing the empty list (nil). Internally this is the atom "nil" encoded as a small atom. It may be copied freely and does not depend on any arena.

Source

pub fn int(i: impl Into<i64>) -> Self

Construct a new integer term. The full 64 bit two’s complement representation of i is stored in the payload. No truncation occurs.

Source

pub fn real(f: impl Into<f64>) -> Self

Construct a new floating point term. The full 64 bit IEEE‑754 bit pattern is stored in the payload without truncation.

Source

pub fn date(ms: impl Into<i64>) -> Self

Construct a new date term representing a Unix epoch in milliseconds. Dates share the same underlying storage as integers but use a distinct tag so they do not compare equal with integer terms.

Source

pub fn atom(arena: &mut Arena, name: impl AsRef<str>) -> Self

Construct or intern an atom into the arena and produce a term referencing it. Small atom names (≤14 bytes of UTF‑8) are inlined directly into the handle; longer names are interned into the arena and referenced by index and length.

Source

pub fn var(arena: &mut Arena, name: impl AsRef<str>) -> Self

Construct or intern a variable into the arena and produce a term referencing it. Small variable names (≤14 bytes) are inlined directly into the handle; longer names are interned in the arena and referenced by index.

Source

pub fn str(arena: &mut Arena, s: impl AsRef<str>) -> Self

Construct or intern a UTF‑8 string into the arena and produce a term referencing it. Strings longer than 14 bytes are interned in the arena; shorter strings are inlined. Invalid UTF‑8 will result in an error.

Source

pub fn bin(arena: &mut Arena, bytes: impl AsRef<[u8]>) -> Self

Construct or intern a binary blob into the arena and produce a term referencing it. Blobs longer than 14 bytes are interned in the arena; shorter blobs are inlined.

Source

pub fn func( arena: &mut Arena, functor: impl AsRef<str>, args: impl IntoIterator<Item = impl IntoTerm>, ) -> Self

Construct a new compound term by interning the functor and arguments in the arena. The returned term references a slice in the arena’s term storage consisting of the functor atom as the first entry followed by the argument handles. A functor of arity zero results in an atom.

Source

pub fn funcv( arena: &mut Arena, terms: impl IntoIterator<Item = impl IntoTerm>, ) -> Result<Self, TermError>

Construct a new compound term by interning the functor and its arguments into the arena as a sequence of terms (functor first, then arguments). A functor with no arguments yields the atom itself. Errors if no functor is provided or if the first term is not an atom.

Source

pub fn list( arena: &mut Arena, terms: impl IntoIterator<Item = impl IntoTerm>, ) -> Self

Constructs a new list. A list is represented internally as an array of terms. If terms is empty, returns nil.

Source

pub fn listc( arena: &mut Arena, terms: impl IntoIterator<Item = impl IntoTerm>, tail: impl IntoTerm, ) -> Self

Constructs a new improper list. An improper list is represented as a list and additional argument. If terms is empty, returns nil.

Source

pub fn tuple( arena: &mut Arena, terms: impl IntoIterator<Item = impl IntoTerm>, ) -> Self

Constructs a new tuple. A tuple is represented internally as an array of terms.

Source

pub fn unpack_int(&self, arena: &Arena) -> Result<i64, TermError>

Returns the value if term is an integer, otherwise an error.

Source

pub fn unpack_real(&self, arena: &Arena) -> Result<f64, TermError>

Returns the value if term is a real, otherwise an error.

Source

pub fn unpack_date(&self, arena: &Arena) -> Result<i64, TermError>

Returns the value if term is a date, otherwise an error.

Source

pub fn unpack_str<'a>(&'a self, arena: &'a Arena) -> Result<&'a str, TermError>

Returns the string slice if term is a string, otherwise an error.

Source

pub fn unpack_bin<'a>(&'a self, arena: &'a Arena) -> Result<&'a [u8], TermError>

Returns the slice if term is a binary blob, otherwise an error.

Source

pub fn unpack_atom<'a>( &'a self, arena: &'a Arena, allowed_names: &[&str], ) -> Result<&'a str, TermError>

Returns the name if term is an atom, otherwise an error.

Source

pub fn unpack_var<'a>( &'a self, arena: &'a Arena, allowed_names: &[&str], ) -> Result<&'a str, TermError>

Returns the name if term is a variable, otherwise an error.

Source

pub fn unpack_func_any<'a>( &'a self, arena: &'a Arena, allowed_names: &[&str], ) -> Result<(&'a Term, &'a [Term]), TermError>

Returns the name and arguments if term is a compound term of any arity or an atom and its name is in allowed_names (or if allowed_names is empty), otherwise returns an error.

Source

pub fn unpack_func<'a, const ARITY: usize>( &'a self, arena: &'a Arena, allowed_names: &[&str], ) -> Result<(&'a Term, [Term; ARITY]), TermError>

Returns the name and arguments if term is a compound term of arity ARITY (or an atom if ARITY == 0) and its name is in allowed_names (or if allowed_names is empty), otherwise returns an error.

Source

pub fn unpack_list<'a>( &'a self, arena: &'a Arena, ) -> Result<(&'a [Term], &'a Term), TermError>

Returns the slice with list elements and the tail if term is a list, otherwise returns an error.

Source

pub fn unpack_tuple_any<'a>( &'a self, arena: &'a Arena, ) -> Result<&'a [Term], TermError>

Returns the slice with tuple elements if term is a tuple of any arity, otherwise returns an error.

Source

pub fn unpack_tuple<const ARITY: usize>( &self, arena: &Arena, ) -> Result<[Term; ARITY], TermError>

Returns the tuple elements if term is a tuple of arity ARITY, otherwise returns an error.

Source

pub fn is_inline(&self) -> bool

Returns true if the value fits directly in Term without arena storage, i.e. int, real, date, or a small atom, var, str, or bin.

Source

pub fn is_func(&self) -> bool

Returns true if the term is a compound term.

Source

pub fn is_list(&self) -> bool

Returns true if the term is a list.

Source

pub fn is_tuple(&self) -> bool

Returns true if the term is a tuple.

Source

pub fn is_int(&self) -> bool

Returns true if the term is an integer.

Source

pub fn is_real(&self) -> bool

Returns true if the term is a real (floating-point) number.

Source

pub fn is_date(&self) -> bool

Returns true if the term is a date.

Source

pub fn is_atom(&self) -> bool

Returns true if the term is an atom.

Source

pub fn is_var(&self) -> bool

Returns true if the term is a variable.

Source

pub fn is_number(&self) -> bool

Returns true if the term is a number (int, real, or date).

Source

pub fn is_str(&self) -> bool

Returns true if the term is a string.

Source

pub fn is_bin(&self) -> bool

Returns true if the term is a binary blob.

Source

pub fn arity(&self) -> usize

Returns the arity of the term. Currently the arity of lists and variables is 0.

Source

pub fn name<'a>(&'a self, arena: &'a Arena) -> Result<&'a str, TermError>

Returns the name of a compound term, atom, or variable. Use [atom_name], [func_name], or [var_name] to ensure the term is of a specific kind.

Source

pub fn atom_name<'a>(&'a self, arena: &'a Arena) -> Result<&'a str, TermError>

Returns the name of an atom,

Source

pub fn var_name<'a>(&'a self, arena: &'a Arena) -> Result<&'a str, TermError>

Returns the name of a variable.

Source

pub fn func_name<'a>(&'a self, arena: &'a Arena) -> Result<&'a str, TermError>

Returns the name of a compund term.

Source

pub fn kind_name(&self) -> &'static str

Returns a string describing the kind of this term.

Source§

impl Term

Source

pub fn view<'a>(&'a self, arena: &'a Arena) -> Result<View<'a>, TermError>

Produce a View of this term that borrows from the given Arena. This method decodes any inlined bytes and dereferences indexes into the arena to yield structured references. See View for details.

Trait Implementations§

Source§

impl AsRef<Term> for Term

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Term

Source§

fn clone(&self) -> Term

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Term

Implements the standard Debug formatter for Term.

This prints a developer-friendly representation of the term, showing its kind (e.g. int, atom, list, tuple) and its internal value in a form useful for debugging.

The output is not guaranteed to be stable across versions and should not be parsed; it is intended purely for diagnostics and logging.

§Example

let t = Term::int(42);
println!("{:?}", t); // e.g. prints `Int(42)`
Source§

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

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

impl From<f32> for Term

Source§

fn from(v: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Term

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Term

Source§

fn from(v: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Term

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Term

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Term

Source§

fn from(v: i8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Term

Source§

fn from(v: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Term

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Term

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl IntoTerm for &Term

Source§

fn into_term(self, _arena: &mut Arena) -> Term

Source§

impl IntoTerm for Term

Source§

fn into_term(self, _arena: &mut Arena) -> Term

Source§

impl PartialEq for Term

Source§

fn eq(&self, other: &Term) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 PartialOrd for Term

Source§

fn partial_cmp(&self, other: &Term) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Term

Source§

impl StructuralPartialEq for Term

Auto Trait Implementations§

§

impl Freeze for Term

§

impl RefUnwindSafe for Term

§

impl Send for Term

§

impl Sync for Term

§

impl Unpin for Term

§

impl UnwindSafe for Term

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V