Enum stackstack::Stack

source ·
pub enum Stack<'a, T> {
    Bottom,
    Top {
        head: T,
        tail: &'a Self,
    },
}
Expand description

A singly-linked list intended to be chained along stack frames.

See module documentation for more.

Variants§

§

Bottom

An empty stack

§

Top

A successor to a previous stack

Fields

§head: T

Implementations§

source§

impl<'a, T> Stack<'a, T>

source

pub const BOTTOM: &'a Self = _

A utility terminator for any stack.

let b = Stack::BOTTOM;
let _: &Stack::<&str> = b;
assert!(b.is_empty());
source

pub const fn of(head: T) -> Self

Returns a stack of length 1, containing only the given item.

let a = Stack::of("a");
assert_eq!(a.len(), 1);
source

pub const fn pushed(&'a self, head: T) -> Self

Return a new stack with the given element appended.

let a = Stack::of("a");
let ab = a.pushed("b");
assert_eq!(a.len(), 1);
assert_eq!(ab.len(), 2);
assert!(ab.iter().eq(&["a", "b"]))
source

pub fn chained( &'a self, iter: impl IntoIterator<Item = &'a mut Self>, ) -> &'a Self

Edit all the non-empty items in the given iterator to follow from self, returning the head of the stack.

This is useful for appending items with some scratch space.

let mut scratch = ["b", "c", "d"].map(Stack::of);
let a = Stack::of("a");
let abcd = a.chained(&mut scratch);
assert!(abcd.iter().eq(&["a", "b", "c", "d"]))
source

pub fn on_chained<R>( &self, chain: impl IntoIterator<Item = T>, on: impl FnOnce(&Stack<'_, T>) -> R, ) -> R

Extend the current stack with each item from the given iterator, calling the given closure on the result.

This creates a stack frame for each item in the iterator.

use stackstack::Stack;
let a = Stack::of("a");
a.on_chained(["b", "c", "d"], |abcd| {
    assert!(abcd.iter().eq(&["a", "b", "c", "d"]))
})
source

pub fn len(&self) -> usize

Return the number of items in the stack.

source

pub fn is_empty(&self) -> bool

Returns true if there are no items in the stack.

source

pub fn get(&self, ix: usize) -> Option<&T>

Get an item by 0-based index, from the bottom of the stack.

let mut storage;
let abcd = stack!(["a", "b", "c", "d"] in storage);
assert_eq!(abcd.get(0), Some(&"a"));
assert_eq!(abcd.get(3), Some(&"d"));
assert_eq!(abcd.get(4), None);
source

pub fn iter(&'a self) -> Iter<'a, T>

Return an Iterator of items in the stack, from the bottom to the top.

Note that the returned item implements DoubleEndedIterator.

let mut storage;
let abcd = stack!(["a", "b", "c", "d"] in storage);
assert!(abcd.iter().eq(&["a", "b", "c", "d"]));
assert!(abcd.iter().rev().eq(&["d", "c", "b", "a"]));

Trait Implementations§

source§

impl<'a, T: Clone> Clone for Stack<'a, T>

source§

fn clone(&self) -> Stack<'a, 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<'a, T: Debug> Debug for Stack<'a, T>

source§

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

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

impl<'a, T> Default for Stack<'a, T>

source§

fn default() -> Self

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

impl<'a, T> IntoIterator for &'a Stack<'a, T>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T: Ord> Ord for Stack<'a, T>

source§

fn cmp(&self, other: &Stack<'a, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<'a, T: PartialEq> PartialEq for Stack<'a, T>

source§

fn eq(&self, other: &Stack<'a, 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<'a, T: PartialOrd> PartialOrd for Stack<'a, T>

source§

fn partial_cmp(&self, other: &Stack<'a, T>) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a, T: Copy> Copy for Stack<'a, T>

source§

impl<'a, T: Eq> Eq for Stack<'a, T>

source§

impl<'a, T> StructuralPartialEq for Stack<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for Stack<'a, T>
where T: Freeze,

§

impl<'a, T> RefUnwindSafe for Stack<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Stack<'a, T>
where T: Send + Sync,

§

impl<'a, T> Sync for Stack<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Stack<'a, T>
where T: Unpin,

§

impl<'a, T> UnwindSafe for Stack<'a, T>

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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, 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.