[][src]Enum stack_list::Node

pub enum Node<'a, T> {
    Root,
    Head {
        data: T,
        tail: &'a Node<'a, T>,
    },
}

A stack-based linked list.

This is a node in a traditional linked list. The main difference is that it does not box the next element, but simply points to it. As a result, it is harder to pass around or to mutate, but has the advantage of not requiring any heap allocation.

Variants

Root

An empty list.

Head

A node in the list with some data and a tail.

Fields of Head

data: T

Some data attached on this node. This is the first element of this list.

tail: &'a Node<'a, T>

The rest of the list as a tail node.

Methods

impl<'a, T> Node<'a, T>[src]

pub fn new() -> Self[src]

Create a new empty list.

pub fn prepend(&'a self, data: T) -> Self[src]

Create a new list by prepending self with data.

pub fn iter(&'a self) -> impl Iterator<Item = &'a T>[src]

Returns an iterator on this list.

pub fn for_each_rev<F>(&self, f: F) where
    F: FnMut(&T), 
[src]

Run the given closure on each item from this list, starting from the end.

pub fn tail(&self) -> Option<&Self>[src]

Returns the next element in the list, if any.

pub fn first(&self) -> Option<&T>[src]

Returns the first data element in this list, if any.

pub fn len(&self) -> usize[src]

Returns the length of this list.

pub fn is_empty(&self) -> bool[src]

Returns true if this list is empty.

pub fn skip(&self, n: usize) -> Option<&Self>[src]

Returns a sublist made by skipping the n first elements.

Returns None if n >= self.len().

pub fn get(&self, n: usize) -> Option<&T>[src]

Returns the data at index n.

Returns None if n >= self.len().

pub fn last(&self) -> Option<&T>[src]

Returns the data for the last item in this list.

Returns None if self is empty.

pub fn fold<F, S>(&self, start: S, f: F) -> S where
    F: FnMut(S, T) -> S,
    T: Clone
[src]

Fold the given function over this list.

Conceptually, runs the code:

This example is not tested
for data in self {
    start = f(start, data);
}
start

pub fn reverse<F, R>(&self, f: F) -> R where
    F: for<'b> FnOnce(&'b Node<'b, T>) -> R,
    R: 'static,
    T: Clone
[src]

Reverse this list.

This does not return a stacklist; instead, it runs f on a reversed version of self.

pub fn prepend_all_rev<I, F, R>(&self, i: I, f: F) -> R where
    I: Iterator<Item = T>,
    F: for<'b> FnOnce(&'b Node<'b, T>) -> R,
    R: 'static, 
[src]

Prepend all the elements from the given iterator, in reverse order.

Examples

stack_list::make!(let a = [3, 4, 5]);
assert_eq!(a.get(0), Some(&3));

a.prepend_all_rev([2, 1].iter().copied(), |b| {
    assert_eq!(b.get(0), Some(&1));
});

pub fn prepend_all<I, F, R>(&self, i: I, f: F) -> R where
    I: DoubleEndedIterator<Item = T>,
    F: for<'b> FnOnce(&'b Node<'b, T>) -> R,
    R: 'static, 
[src]

Prepend all the elements from the given iterator.

Examples

stack_list::make!(let a = [3, 4, 5]);
assert_eq!(a.get(0), Some(&3));

a.prepend_all([1, 2].iter().copied(), |b| {
    assert_eq!(b.get(0), Some(&1));
});

pub fn from_rev_iterator<I, F, R>(i: I, f: F) -> R where
    I: Iterator<Item = T>,
    F: for<'b> FnOnce(&'b Node<'b, T>) -> R,
    R: 'static, 
[src]

Build a stacklist using the items from the iterator in reverse order.

Note: this does not return the stacklist. Instead, it calls the given closure with the generated list.

pub fn from_iterator<I, F, R>(i: I, f: F) -> R where
    I: DoubleEndedIterator<Item = T>,
    F: for<'b> FnOnce(&'b Node<'b, T>) -> R,
    R: 'static, 
[src]

Build a stacklist using the items from the iterator.

Note: this does not return the stacklist. Instead, it calls the given closure with the generated list.

Trait Implementations

impl<'a, T: Debug> Debug for Node<'a, T>[src]

impl<'a, T: PartialEq> PartialEq<Node<'a, T>> for Node<'a, T>[src]

impl<'a, T: Eq> Eq for Node<'a, T>[src]

impl<'a, T: Copy> Copy for Node<'a, T>[src]

impl<'a, T> StructuralPartialEq for Node<'a, T>[src]

impl<'a, T> StructuralEq for Node<'a, T>[src]

impl<'b, T> Iterator for &'b Node<'b, T>[src]

type Item = &'b T

The type of the elements being iterated over.

impl<'a, T: Clone> Clone for Node<'a, T>[src]

Auto Trait Implementations

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

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

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

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]