Struct ExitStack

Source
pub struct ExitStack<Mem> { /* private fields */ }
Expand description

Provides memory suitable for dynamically stack-allocating pinned values.

Alternatively, you can also use this as a pre-allocated buffer for pinning values to the heap. The internal implementation guarantees that the stack can allocate at least one value of type Mem. Also, there will be no changes that reduce the amount of values in a patch version, it will require require at least a minor version bump.

Implementations§

Source§

impl<Mem> ExitStack<Mem>

Source

pub fn new() -> Self

Create an empty exit stack.

The available memory for the stack is specified by the Mem type parameter.

Source

pub fn slot<'stack, T: 'static>( self: Pin<&'stack Self>, ) -> Option<Slot<'stack, T>>

Prepare a slot for pinning a value to the stack.

Note that dropping may be delayed arbitrarily since this method can’t control when the ExitStack itself is dropped. Thus the pinned value must not borrow temporary data.

Source

pub fn set<'stack>( self: Pin<&'stack mut Self>, val: Mem, ) -> Pin<&'stack mut Mem>
where Mem: 'static,

Infallibly get a slot for the type of the memory, and pin a value.

This is useful a small utility wrapper if you want to have a small ‘stack’ that can make room for a single entry, on demand. All other entries in the exit stack will be popped, and the memory allocator will be cleared before the new value is pinned.

You can also use this method to defer dropping of a pinned task beyond your own method’s body.

§Usage
use core::pin::Pin;
use exit_stack::ExitStack;

// Some async future that is not Unpin.
#[derive(Default)]
struct Task {
    // ..
}


async fn with_stack(mut stack: Pin<&mut ExitStack<Task>>) {
    stack.as_mut().set(Task::default()).await;
    // Reuse the stack another time.
    // The previous task is dropped.
    stack.as_mut().set(Task::default()).await;
    // Note that the second task still lives when we return.
}
Source

pub fn pop_all(self: Pin<&mut Self>)

Drop all values, resetting the stack.

Trait Implementations§

Source§

impl<Mem> Drop for ExitStack<Mem>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<Mem> !Freeze for ExitStack<Mem>

§

impl<Mem> !RefUnwindSafe for ExitStack<Mem>

§

impl<Mem> !Send for ExitStack<Mem>

§

impl<Mem> !Sync for ExitStack<Mem>

§

impl<Mem> Unpin for ExitStack<Mem>
where Mem: Unpin,

§

impl<Mem> !UnwindSafe for ExitStack<Mem>

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.