Enum TermValue

Source
pub enum TermValue {
Show 14 variants SmallInt(i32), Atom(AtomIndex), Nil, Pid(ProcessId), Port(PortId), Reference(RefId), Tuple(Vec<TermValue>), List(Box<TermValue>, Box<TermValue>), Map(Vec<(TermValue, TermValue)>), Binary(Vec<u8>), Function(FunctionRef), Resource(ResourceRef), Float(f64), Invalid,
}
Expand description

Clean, functional ADT for AtomVM terms

This is completely generic - it works with any atom table implementation through the AtomTableOps trait.

Variants§

§

SmallInt(i32)

§

Atom(AtomIndex)

§

Nil

§

Pid(ProcessId)

§

Port(PortId)

§

Reference(RefId)

§

Tuple(Vec<TermValue>)

§

List(Box<TermValue>, Box<TermValue>)

§

Map(Vec<(TermValue, TermValue)>)

§

Binary(Vec<u8>)

§

Function(FunctionRef)

§

Resource(ResourceRef)

§

Float(f64)

§

Invalid

Implementations§

Source§

impl TermValue

Source

pub fn as_int(&self) -> Option<i32>

Pattern match on integers

Source

pub fn as_atom(&self) -> Option<AtomIndex>

Pattern match on atoms

Source

pub fn as_tuple(&self) -> Option<&[TermValue]>

Pattern match on tuples

Source

pub fn as_list(&self) -> Option<(&TermValue, &TermValue)>

Pattern match on lists (functional style)

Source

pub fn is_nil(&self) -> bool

Check if this is nil

Source

pub fn is_empty_list(&self) -> bool

Check if this is an empty list

Source

pub fn fold_list<T, F>(&self, init: T, f: F) -> T
where F: Fn(T, &TermValue) -> T,

Fold over list elements (functional programming!)

Source

pub fn map_list<F>(&self, f: F) -> TermValue
where F: Fn(&TermValue) -> TermValue + Clone,

Map over list elements

Source

pub fn filter_list<F>(&self, predicate: F) -> TermValue
where F: Fn(&TermValue) -> bool + Clone,

Filter list elements

Source

pub fn list_length(&self) -> usize

Get list length

Source

pub fn list_to_vec(&self) -> Vec<TermValue>

Convert list to Vec

Source

pub fn map_get(&self, key: &TermValue) -> Option<&TermValue>

Get map value by key (functional lookup)

Source

pub fn map_set(&self, key: TermValue, value: TermValue) -> TermValue

Set map value (returns new map)

Source

pub fn from_iter<I>(iter: I) -> TermValue

Construct list from iterator (functional construction)

Source

pub fn from_vec(elements: Vec<TermValue>) -> TermValue

Construct proper list from Vec

Source§

impl TermValue

Source

pub fn int(value: i32) -> Self

Source

pub fn atom<T: AtomTableOps>(name: &str, table: &T) -> Self

Create atom using any atom table (GENERIC!)

Source

pub fn tuple(elements: Vec<TermValue>) -> Self

Source

pub fn list(elements: Vec<TermValue>) -> Self

Source

pub fn binary(data: Vec<u8>) -> Self

Source

pub fn map(pairs: Vec<(TermValue, TermValue)>) -> Self

Source

pub fn pid(id: u32) -> Self

Source

pub fn port(id: u32) -> Self

Source

pub fn reference(id: u64) -> Self

Source

pub fn float(value: f64) -> Self

Source§

impl TermValue

Source

pub fn to_int_or(&self, default: i32) -> i32

Extract integer with default

Source

pub fn tuple_get(&self, index: usize) -> Option<&TermValue>

Extract tuple element by index

Source

pub fn tuple_arity(&self) -> usize

Extract tuple arity

Source

pub fn sum_list(&self) -> i32

Example: Sum all integers in a list

Source

pub fn double_ints(&self) -> TermValue

Example: Convert list of integers to list of their doubles

Source

pub fn is_atom_str<T: AtomTableOps>(&self, name: &str, table: &T) -> bool

Check if atom matches string using any atom table (GENERIC!)

Source

pub fn as_atom_str<T: AtomTableOps>(&self, table: &T) -> Option<String>

Get atom as string using any atom table (GENERIC!)

Trait Implementations§

Source§

impl Clone for TermValue

Source§

fn clone(&self) -> TermValue

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 TermValue

Source§

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

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

impl PartialEq for TermValue

Source§

fn eq(&self, other: &TermValue) -> 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 StructuralPartialEq for TermValue

Auto Trait Implementations§

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.