pub struct Atom<'a>(/* private fields */);Expand description
A reference to an expression node allocated in an arena.
Atom is a small copyable handle. The actual node data lives in the
Arena and is freed when the arena is dropped.
§Example
use ocas_atom::AtomArena;
use ocas_core::arena::Arena;
let arena = Arena::new();
let ctx = AtomArena::new(&arena);
let x = ctx.var("x");
let two = ctx.num(2);
let expr = ctx.pow(x, two);
assert_eq!(expr.to_string(), "x^2");Implementations§
Source§impl<'a> Atom<'a>
impl<'a> Atom<'a>
Sourcepub fn node(&self) -> &'a AtomNode<'a>
pub fn node(&self) -> &'a AtomNode<'a>
Access the underlying node data.
§Example
use ocas_atom::{AtomArena, AtomNode};
use ocas_core::arena::Arena;
let arena = Arena::new();
let ctx = AtomArena::new(&arena);
let x = ctx.var("x");
assert!(matches!(x.node(), AtomNode::Var(_)));Sourcepub fn children(&self) -> &'a [Atom<'a>]
pub fn children(&self) -> &'a [Atom<'a>]
Returns the direct children of this atom, in left-to-right order.
Num, Var, and Pow report no children through this API; use
Self::binary_children for the two operands of Pow.
§Example
use ocas_atom::AtomArena;
use ocas_core::arena::Arena;
let arena = Arena::new();
let ctx = AtomArena::new(&arena);
let x = ctx.var("x");
let y = ctx.var("y");
let sum = ctx.add(&[x, y, ctx.num(1)]);
assert_eq!(sum.children().len(), 3);Sourcepub fn binary_children(&self) -> Option<(Atom<'a>, Atom<'a>)>
pub fn binary_children(&self) -> Option<(Atom<'a>, Atom<'a>)>
If this atom is a binary operator, return its two operands.
§Example
use ocas_atom::AtomArena;
use ocas_core::arena::Arena;
let arena = Arena::new();
let ctx = AtomArena::new(&arena);
let x = ctx.var("x");
let y = ctx.var("y");
let power = ctx.pow(x, y);
let (base, exp) = power.binary_children().unwrap();
assert_eq!(base.to_string(), "x");
assert_eq!(exp.to_string(), "y");Trait Implementations§
impl<'a> Copy for Atom<'a>
impl<'a> Eq for Atom<'a>
Source§impl<'a> Ord for Atom<'a>
impl<'a> Ord for Atom<'a>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<'a> PartialOrd for Atom<'a>
impl<'a> PartialOrd for Atom<'a>
impl<'a> StructuralPartialEq for Atom<'a>
Auto Trait Implementations§
impl<'a> Freeze for Atom<'a>
impl<'a> RefUnwindSafe for Atom<'a>
impl<'a> Send for Atom<'a>
impl<'a> Sync for Atom<'a>
impl<'a> Unpin for Atom<'a>
impl<'a> UnsafeUnpin for Atom<'a>
impl<'a> UnwindSafe for Atom<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more