pub struct Stack<T> { /* private fields */ }
Expand description
A growable and shrinkable stack array type.
§Example
use hay::Stack;
let mut stack = Stack::new();
stack.push(1);
stack.push(2);
assert_eq!(stack.len(), 2);
assert_eq!(stack.pop(), Some(2));
assert_eq!(stack.pop(), Some(1));
assert_eq!(stack.pop(), None);
~24 bytes on the stack!!!
Implementations§
Source§impl<T> Stack<T>
impl<T> Stack<T>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs a new, empty Stack<T>
.
The stack will not allocate until elements are pushed onto it.
§Example
use hay::Stack;
let mut stack: Stack<i32> = Stack::<i32>::new();
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the stack, popping all values.
Note that this method has no effect on the allocated capacity of the stack.
§Example
use hay::Stack;
let mut stack = Stack::new();
stack.push(1);
stack.push(2);
stack.push(3);
stack.clear();
assert_eq!(stack.pop(), None);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements on the stack, also referred to as it’s ‘length’.
§Example
use hay::Stack;
let mut stack = Stack::new();
stack.push(1);
stack.push(2);
assert_eq!(stack.len(), 2);
Sourcepub const fn as_vec(&self) -> &Vec<T>
pub const fn as_vec(&self) -> &Vec<T>
Extracts a vector containing the entire stack.
§Example
use hay::Stack;
let mut stack = Stack::new();
stack.push(0);
stack.push(1);
stack.push(2);
let slice = unsafe { stack.as_vec().as_slice() };
assert_eq!(slice, &[0, 1, 2]);
Sourcepub fn as_mut_vec(&mut self) -> &mut Vec<T>
pub fn as_mut_vec(&mut self) -> &mut Vec<T>
Extracts a mutable vector containing the entire stack.
§Example
use hay::Stack;
let mut stack = Stack::new();
stack.push(2);
unsafe { stack.as_mut_vec().insert(0, 1) };
assert_eq!(stack.len(), 2);
assert_eq!(stack.pop(), Some(2));
assert_eq!(stack.pop(), Some(1));
assert_eq!(stack.pop(), None);
Trait Implementations§
Source§impl<T: Ord> Ord for Stack<T>
impl<T: Ord> Ord for Stack<T>
Source§impl<T: PartialOrd> PartialOrd for Stack<T>
impl<T: PartialOrd> PartialOrd for Stack<T>
impl<T: Eq> Eq for Stack<T>
impl<T> StructuralPartialEq for Stack<T>
Auto Trait Implementations§
impl<T> Freeze for Stack<T>
impl<T> RefUnwindSafe for Stack<T>where
T: RefUnwindSafe,
impl<T> Send for Stack<T>where
T: Send,
impl<T> Sync for Stack<T>where
T: Sync,
impl<T> Unpin for Stack<T>where
T: Unpin,
impl<T> UnwindSafe for Stack<T>where
T: UnwindSafe,
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
Mutably borrows from an owned value. Read more