pub struct DynStack<T: ?Sized> { /* private fields */ }Implementations§
Source§impl<T: ?Sized> DynStack<T>
impl<T: ?Sized> DynStack<T>
Sourcepub const unsafe fn new_unchecked() -> Self
pub const unsafe fn new_unchecked() -> Self
Creates a new, empty, DynStack. This method is a const fn, so instances can be
statically initialized. This comes at the cost of no runtime sanity check that the stack
is properly used with trait objects, which is why it is unsafe to call.
§Safety
Must only be called with the generic, T, being a trait object.
§Example
// SAFETY: Storing trait objects, as required
static CORRECT: DynStack<dyn Display + Sync> = unsafe { DynStack::new_unchecked() };
// DONT do the following, Sting is not a trait object. This compiles
// but has undefined behavior.
static WRONG: DynStack<String> = unsafe { DynStack::new_unchecked() };Storing it directly in a static, like above, does of course not make much sense, because
you can’t modify it. But if you put it inside a synchronazation primitive that allows
static initialization, such as parking_lot::RwLock, it can be used as a globally
accessible, modifiable stack.
Sourcepub unsafe fn push(&mut self, item: *mut T)
pub unsafe fn push(&mut self, item: *mut T)
Push a trait object onto the stack.
This method is unsafe because in lieu of moving a trait object onto push’s stack
(not possible in rust as of 1.30.0) we copy it from the provided mutable pointer.
The user of this method must therefore either ensure that item has no Drop impl,
or explicitly call std::mem::forget on item after pushing.
It is highly recommended to use the dyn_push macro instead of calling this directly.
Sourcepub fn remove_last(&mut self) -> bool
pub fn remove_last(&mut self) -> bool
Remove the last trait object from the stack. Returns true if any items were removed.
Sourcepub fn get<'a>(&'a self, index: usize) -> Option<&'a T>
pub fn get<'a>(&'a self, index: usize) -> Option<&'a T>
Retrieve a trait object reference at the provided index.
Sourcepub fn get_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut T>
pub fn get_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut T>
Retrieve a mutable trait object reference at the provided index.
Sourcepub fn peek<'a>(&'a self) -> Option<&'a T>
pub fn peek<'a>(&'a self) -> Option<&'a T>
Retrieve the trait object reference at the top of the stack.