Struct stack_dst::stack::Stack

source ·
pub struct Stack<T: ?Sized, D: DataBuf> { /* private fields */ }
Expand description

A fixed-capacity stack that can contain dynamically-sized types

Uses an array of usize as a backing store for a First-In, Last-Out stack of items that can unsize to T.

Note: Each item in the stack takes at least one slot in the buffer (to store the metadata)

Implementations§

source§

impl<T: ?Sized, D: DataBuf> Stack<T, D>

source

pub fn new() -> Selfwhere D: Default,

Construct a new (empty) stack

source

pub fn with_buffer(data: D) -> Self

Construct a new (empty) stack using the provided buffer

source

pub fn is_empty(&self) -> bool

Tests if the stack is empty

source

pub fn push<U: Unsize<T>>(&mut self, v: U) -> Result<(), U>where (U, D::Inner): AlignmentValid,

Push a value at the top of the stack

let mut stack = Stack::<[u8], ::stack_dst::buffers::U64_8>::new();
stack.push([1, 2, 3]);
source

pub fn push_stable<U, F: FnOnce(&U) -> &T>( &mut self, v: U, f: F ) -> Result<(), U>where (U, D::Inner): AlignmentValid,

Push a value at the top of the stack (without using Unsize)

let mut stack = Stack::<[u8], ::stack_dst::buffers::U64_8>::new();
stack.push_stable([1, 2,3], |v| v);
source

pub fn top(&self) -> Option<&T>

Returns a pointer to the top item on the stack

source

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

Returns a pointer to the top item on the stack (unique/mutable)

source

pub fn pop(&mut self)

Pop the top item off the stack

source

pub fn iter(&self) -> Iter<'_, T, D>

Obtain an immutable iterator (yields references to items, in the order they would be popped)

let mut list = ::stack_dst::Stack::<str, ::stack_dst::buffers::Ptr8>::new();
list.push_str("Hello");
list.push_str("world");
let mut it = list.iter();
assert_eq!(it.next(), Some("world"));
assert_eq!(it.next(), Some("Hello"));
assert_eq!(it.next(), None);
source

pub fn iter_mut(&mut self) -> IterMut<'_, T, D>

Obtain unique/mutable iterator

let mut list = ::stack_dst::Stack::<[u8], ::stack_dst::buffers::Ptr8>::new();
list.push_copied(&[1,2,3]);
list.push_copied(&[9]);
for v in list.iter_mut() {
    v[0] -= 1;
}
let mut it = list.iter();
assert_eq!(it.next(), Some(&[8][..]));
assert_eq!(it.next(), Some(&[0,2,3][..]));
assert_eq!(it.next(), None);
source§

impl<D: DataBuf> Stack<str, D>

source

pub fn push_str(&mut self, v: &str) -> Result<(), ()>

Push the contents of a string slice as an item onto the stack

let mut stack = Stack::<str, ::stack_dst::buffers::U8_32>::new();
stack.push_str("Hello!");
source§

impl<D: DataBuf, T: Clone> Stack<[T], D>where (T, D::Inner): AlignmentValid,

source

pub fn push_cloned(&mut self, v: &[T]) -> Result<(), ()>

Pushes a set of items (cloning out of the input slice)

let mut stack = Stack::<[u8], ::stack_dst::buffers::U64_8>::new();
stack.push_cloned(&[1, 2, 3]);
source

pub fn push_copied(&mut self, v: &[T]) -> Result<(), ()>where T: Copy,

Pushes a set of items (copying out of the input slice)

let mut stack = Stack::<[u8], ::stack_dst::buffers::U64_8>::new();
stack.push_copied(&[1, 2, 3]);
source§

impl<D: DataBuf, T> Stack<[T], D>where (T, D::Inner): AlignmentValid,

source

pub fn push_from_iter( &mut self, iter: impl ExactSizeIterator<Item = T> ) -> Result<(), ()>

Push an item, populated from an exact-sized iterator

let mut stack = Stack::<[u8], stack_dst::buffers::Ptr8>::new();
stack.push_from_iter(0..10);
assert_eq!(stack.top().unwrap(), &[0,1,2,3,4,5,6,7,8,9]);

Trait Implementations§

source§

impl<D: DataBuf, T> Debug for Stack<T, D>where T: Debug + ?Sized,

source§

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

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

impl<T: ?Sized, D: DataBuf + Default> Default for Stack<T, D>

source§

fn default() -> Self

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

impl<T: ?Sized, D: DataBuf> Drop for Stack<T, D>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T: ?Sized, D> RefUnwindSafe for Stack<T, D>where D: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, D> !Send for Stack<T, D>

§

impl<T, D> !Sync for Stack<T, D>

§

impl<T: ?Sized, D> Unpin for Stack<T, D>where D: Unpin,

§

impl<T: ?Sized, D> UnwindSafe for Stack<T, D>where D: UnwindSafe, T: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.