Crate astack

Source
Expand description

astack offers a Stack data structure with fixed capacity capable of fast LIFO operations.

The crate contains:

  • A Stack struct, the main object used to perform TOS operations.
  • A stack macro, used to conveniently construct a new Stack.
  • A StackError used by the Stack to signal an invalid operation has occurred.
  • A StackIntoIter struct used to iterate over the values of a Stack.

astack may be used in both std and non-std environments, and is therefore considered platform-agnostic. The crate does not require an allocator at all!

You can optionally enable the std feature to use some additional features, such as the std::error::Error implementation for StackError.

§Examples

§Creating an empty Stack

use astack::{stack, Stack};

// Through the Stack::new() method
let stack = Stack::<u64, 10>::new();

// Through the stack! macro
let stack = stack![u64; 10];

§Creating a Stack with already some items inside

use astack::stack;

// Through the stack! macro
let stack = stack! {
    [u64; 10] = [1, 2, 3]
};

§Creating a Stack filled with items

use astack::Stack;

// A Stack filled with one single Copy item.
let stack_of_44 = Stack::<u64, 10>::fill_with_copy(44);

// A Stack filled with the Default implementation of u64.
let stack_of_default = Stack::<u64, 10>::fill_with_default();

// A Stack filled with a value based on the result of a function.
let stack = Stack::<String, 10>::fill_with_fn(|i| {
    format!("Value n. {}", i)
});

§Common Stack operations

use astack::stack;

// Create a new Stack.
let mut stack = stack! {
    [i32; 4] = [10, 20, 30]
};

// Add an item as TOS. This returns Err if the stack if full.
stack.push(40).unwrap();

// Pop TOS. This returns None if the stack is empty.
let last_value = stack.pop().unwrap();

// Get a reference to TOS. This returns None if the stack is empty.
assert_eq!(stack.tos(), Some(&30));

Macros§

stack
A macro for conveniently creating a Stack.

Structs§

Stack
A data structure with fixed capacity aimed at fast LIFO operations.
StackIntoIter
A helper struct used to iterate over the items contained in a Stack from top to bottom with the syntax for var in expr { /* code */ }.

Enums§

StackError
The type returned by Stack’s checked operations if they fail.