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>
impl<T: ?Sized, D: DataBuf> Stack<T, D>
sourcepub fn with_buffer(data: D) -> Self
pub fn with_buffer(data: D) -> Self
Construct a new (empty) stack using the provided buffer
sourcepub fn push<U: Unsize<T>>(&mut self, v: U) -> Result<(), U>where
(U, D::Inner): AlignmentValid,
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]);
sourcepub fn push_stable<U, F: FnOnce(&U) -> &T>(
&mut self,
v: U,
f: F
) -> Result<(), U>where
(U, D::Inner): AlignmentValid,
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);
sourcepub fn top_mut(&mut self) -> Option<&mut T>
pub fn top_mut(&mut self) -> Option<&mut T>
Returns a pointer to the top item on the stack (unique/mutable)
sourcepub fn iter(&self) -> Iter<'_, T, D> ⓘ
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);
sourcepub fn iter_mut(&mut self) -> IterMut<'_, T, D> ⓘ
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, T: Clone> Stack<[T], D>where
(T, D::Inner): AlignmentValid,
impl<D: DataBuf, T: Clone> Stack<[T], D>where (T, D::Inner): AlignmentValid,
source§impl<D: DataBuf, T> Stack<[T], D>where
(T, D::Inner): AlignmentValid,
impl<D: DataBuf, T> Stack<[T], D>where (T, D::Inner): AlignmentValid,
sourcepub fn push_from_iter(
&mut self,
iter: impl ExactSizeIterator<Item = T>
) -> Result<(), ()>
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§
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> 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