Struct cactus::Cactus

source ·
pub struct Cactus<T> { /* private fields */ }
Expand description

An immutable cactus stack node. May be empty or contain a value; may have a pointer to a parent or not.

Implementations§

source§

impl<T> Cactus<T>

source

pub fn new() -> Cactus<T>

Return an empty cactus stack node.

source

pub fn is_empty(&self) -> bool

Is this cactus stack node empty?

§Examples
use cactus::Cactus;
let c = Cactus::new();
assert!(c.is_empty());
let c2 = c.child(1);
assert!(!c2.is_empty());
source

pub fn len(&self) -> usize

How many items are there in this cactus stack?

source

pub fn child(&self, val: T) -> Cactus<T>

Create a new cactus stack node containing value val and pointing to parent self.

§Examples
use cactus::Cactus;
let c = Cactus::new();
let c2 = c.child(1);
let c3 = c2.child(2);
assert_eq!(c3.vals().cloned().collect::<Vec<_>>(), [2, 1]);
source

pub fn parent(&self) -> Option<Cactus<T>>

Return this cactus stack node’s parent node or None if this cactus stack is empty.

§Examples
use cactus::Cactus;
let c = Cactus::new();
let c2 = c.child(1);
assert_eq!(c.parent(), None);
assert_eq!(c2.val(), Some(&1));
assert_eq!(c2.parent().unwrap(), Cactus::new());
source

pub fn val(&self) -> Option<&T>

Return a reference to this cactus stack node’s value or None if this cactus stack is empty.

§Examples
use cactus::Cactus;
let c = Cactus::new().child(1);
assert_eq!(c.val(), Some(&1));
assert_eq!(c.parent().unwrap().val(), None);
source

pub fn nodes(&self) -> CactusNodesIter<'_, T>

Return an iterator over this cactus stack’s nodes. Note that the iterator produces nodes starting from this node and then walking up towards the root.

§Examples
use cactus::Cactus;
let c = Cactus::new().child(1).child(2).child(3);
assert_eq!(c.nodes().skip(1).next(), Some(Cactus::new().child(1).child(2)));
source

pub fn vals(&self) -> CactusValsIter<'_, T>

Return an iterator over this cactus stack’s values. Note that the iterator produces values starting from this node and then walking up towards the root.

§Examples
use cactus::Cactus;
let c = Cactus::new().child(1).child(2).child(3);
assert_eq!(c.vals().cloned().collect::<Vec<_>>(), [3, 2, 1]);
source

pub fn try_unwrap(self) -> Result<T, Cactus<T>>

Try to consume this Cactus node and return its data. If the cactus node has no children, this succeeds; if the cactus node has children, it fails, and returns the original cactus node.

§Examples
use cactus::Cactus;
let c = Cactus::new().child(1).child(2);
let p = c.parent().unwrap();
assert_eq!(c.try_unwrap().unwrap(), 2);
// At this point the c variable can no longer be referenced (its value has moved).
assert_eq!(p.val(), Some(&1));

let d = Cactus::new().child(1);
let d1 = d.child(2);
let d2 = d.child(3);
// At this point d.try_unwrap().unwrap() would return an Err, as d has two children that
// prevent the underlying Cactus from being consumed. We then need to manually clone the
// value if we want to access it uniformly.
assert_eq!(d.try_unwrap().unwrap_or_else(|c| c.val().unwrap().clone()), 1);
// At this point the d variable can no loner be referenced (its value has moved),
// but we can still access the contents it once pointed to:
assert_eq!(*d1.parent().unwrap().val().unwrap(), 1);

Trait Implementations§

source§

impl<T: Clone> Clone for Cactus<T>

source§

fn clone(&self) -> Cactus<T>

Returns a copy 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<T: Debug> Debug for Cactus<T>

source§

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

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

impl<T: Default> Default for Cactus<T>

source§

fn default() -> Cactus<T>

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

impl<T: Hash> Hash for Cactus<T>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T: PartialEq> PartialEq for Cactus<T>

source§

fn eq(&self, other: &Cactus<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Eq> Eq for Cactus<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Cactus<T>
where T: RefUnwindSafe,

§

impl<T> !Send for Cactus<T>

§

impl<T> !Sync for Cactus<T>

§

impl<T> Unpin for Cactus<T>

§

impl<T> UnwindSafe for Cactus<T>
where T: RefUnwindSafe,

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

§

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

§

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

§

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.