Skip to main content

Stack

Struct Stack 

Source
pub struct Stack<T> { /* private fields */ }
Expand description

Allocate memory but never free it until the entire Stack is dropped. Items popped from the stack are marked as Empty so the memory is reused.

Implementations§

Source§

impl<T> Stack<T>

Source

pub const fn empty() -> Self

Create an empty stack

Source

pub fn next_index(&self) -> usize

Get the next index that will be written to

Source

pub fn with_capacity(cap: usize) -> Self

Create a stack with an initial capacity. This will fill the stack with empty entries

Source

pub fn push(&mut self, value: T)

Push a value onto the stack

Source

pub fn pop(&mut self) -> Option<T>

Pop a value off the stack

Source

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

Source

pub fn swap(&mut self, index: usize, new_value: T) -> T

Swap out a value in the stack at a given location.

§Panics

Panics if the index contains an empty slot

Source

pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> + '_

Create an iterator over the values on the stack

Source

pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> + '_

Create an iterator over the values on the stack

Source

pub fn drain( &mut self, ) -> StackDrain<T, impl DoubleEndedIterator<Item = T> + '_>

A draining iterator over the values on the stack.

let mut stack = Stack::empty();
stack.push(1);
stack.push(2);

assert_eq!(stack.drain().next(), Some(2));
assert!(stack.is_empty());
Source

pub fn clear(&mut self)

Clear the values from the stack

Source

pub fn is_empty(&self) -> bool

The stack will contains allocated memory even if is_empty returns true.k

Source

pub fn len(&self) -> usize

Source

pub fn reserve(&mut self, len: usize)

Source

pub fn drain_into(&mut self, local: &mut Stack<T>)

Drain all the values into another stack. Prefer Self::drain_copy_into if T is Copy. It might be marginally faster.

Source§

impl<T: PartialEq> Stack<T>

Source

pub fn contains(&self, value: &T) -> bool

Check if the stack contains a given value

Source§

impl<T: Copy> Stack<T>

Source

pub fn drain_copy_into(&mut self, local: &mut Stack<T>)

Drain the values into another stack. This function can be marginally faster than Self::drain_into but depends on T being Copy.

Trait Implementations§

Source§

impl<T: Debug> Debug for Stack<T>

Source§

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

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

impl<T: Default> Default for Stack<T>

Source§

fn default() -> Stack<T>

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

impl<T> FromIterator<T> for Stack<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more

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> UnsafeUnpin for Stack<T>

§

impl<T> UnwindSafe for Stack<T>
where T: UnwindSafe,

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> 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>,

Source§

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>,

Source§

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.