Struct DynStack

Source
pub struct DynStack<T: ?Sized> { /* private fields */ }

Implementations§

Source§

impl<T: ?Sized> DynStack<T>

Source

pub fn new() -> Self

Creates a new, empty, DynStack.

§Panics

Panics if T is not a trait object.

Source

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.

Source

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.

Source

pub fn remove_last(&mut self) -> bool

Remove the last trait object from the stack. Returns true if any items were removed.

Source

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

Retrieve a trait object reference at the provided index.

Source

pub fn get_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut T>

Retrieve a mutable trait object reference at the provided index.

Source

pub fn peek<'a>(&'a self) -> Option<&'a T>

Retrieve the trait object reference at the top of the stack.

Source

pub fn peek_mut<'a>(&'a mut self) -> Option<&'a mut T>

Retrieve the mutable trait object reference at the top of the stack.

Source

pub fn len(&self) -> usize

Returns the number of trait objects stored on the stack.

Source§

impl<'a, T: 'a + ?Sized> DynStack<T>

Source

pub fn iter(&'a self) -> DynStackIter<'a, T>

Returns an iterator over trait object references

Source

pub fn iter_mut(&'a mut self) -> DynStackIterMut<'a, T>

Returns an iterator over mutable trait object references

Trait Implementations§

Source§

impl<T: ?Sized> Drop for DynStack<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: ?Sized> Index<usize> for DynStack<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, idx: usize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: ?Sized> IndexMut<usize> for DynStack<T>

Source§

fn index_mut(&mut self, idx: usize) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T: 'a + ?Sized> IntoIterator for &'a DynStack<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = DynStackIter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T: 'a + ?Sized> IntoIterator for &'a mut DynStack<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = DynStackIterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: ?Sized + Send> Send for DynStack<T>

Source§

impl<T: ?Sized + Sync> Sync for DynStack<T>

Auto Trait Implementations§

§

impl<T> Freeze for DynStack<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for DynStack<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Unpin for DynStack<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for DynStack<T>
where T: UnwindSafe + ?Sized,

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.