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§
Implementations§
source§impl<'a, T> Stack<'a, T>
impl<'a, T> Stack<'a, T>
sourcepub const BOTTOM: &'a Self = _
pub const BOTTOM: &'a Self = _
A utility terminator for any stack.
let b = Stack::BOTTOM;
let _: &Stack::<&str> = b;
assert!(b.is_empty());sourcepub const fn of(head: T) -> Self
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);
sourcepub const fn pushed(&'a self, head: T) -> Self
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"]))sourcepub fn chained(
&'a self,
iter: impl IntoIterator<Item = &'a mut Self>,
) -> &'a Self
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"]))sourcepub fn on_chained<R>(
&self,
chain: impl IntoIterator<Item = T>,
on: impl FnOnce(&Stack<'_, T>) -> R,
) -> R
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"]))
})sourcepub fn get(&self, ix: usize) -> Option<&T>
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);sourcepub fn iter(&'a self) -> Iter<'a, T> ⓘ
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> IntoIterator for &'a Stack<'a, T>
impl<'a, T> IntoIterator for &'a Stack<'a, T>
source§impl<'a, T: Ord> Ord for Stack<'a, T>
impl<'a, T: Ord> Ord for Stack<'a, T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'a, T: PartialEq> PartialEq for Stack<'a, T>
impl<'a, T: PartialEq> PartialEq for Stack<'a, T>
source§impl<'a, T: PartialOrd> PartialOrd for Stack<'a, T>
impl<'a, T: PartialOrd> PartialOrd for Stack<'a, T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl<'a, T: Copy> Copy for Stack<'a, T>
impl<'a, T: Eq> Eq for Stack<'a, T>
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>
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>where
T: UnwindSafe + RefUnwindSafe,
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
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)