Struct ShutdownManager

Source
pub struct ShutdownManager<T: Clone> { /* private fields */ }
Expand description

Shutdown manager for asynchronous tasks and futures.

The shutdown manager allows you to:

  • Signal futures to shutdown or forcibly cancel them (by dropping them).
  • Wait for futures to perform their clean-up after a shutdown was triggered.
  • Retrieve the shutdown reason after the shutdown was triggered.

The shutdown manager can be cloned and shared with multiple tasks. Each clone uses the same internal state.

Implementations§

Source§

impl<T: Clone> ShutdownManager<T>

Source

pub fn new() -> Self

Create a new shutdown manager.

Source

pub fn is_shutdown_triggered(&self) -> bool

Check if the shutdown has been triggered.

Source

pub fn is_shutdown_completed(&self) -> bool

Check if the shutdown has completed.

Source

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

Get the shutdown reason, if the shutdown has been triggered.

Returns None if the shutdown has not been triggered yet.

Source

pub fn wait_shutdown_triggered(&self) -> ShutdownSignal<T>

Asynchronously wait for the shutdown to be triggered.

This returns a future that completes when the shutdown is triggered. The future can be cloned and sent to other threads or tasks freely.

If the shutdown is already triggered, the returned future immediately resolves.

You can also use ShutdownSignal::wrap_cancel() of the returned object to automatically cancel a future when the shutdown signal is received. This is identical to Self::wrap_cancel().

Source

pub fn wait_shutdown_complete(&self) -> ShutdownComplete<T>

Asynchronously wait for the shutdown to complete.

This returns a future that completes when the shutdown is complete. The future can be cloned and sent to other threads or tasks freely.

The shutdown is complete when all DelayShutdownToken are dropped and all WrapDelayShutdown futures have completed or are dropped.

Source

pub fn trigger_shutdown( &self, reason: T, ) -> Result<(), ShutdownAlreadyStarted<T>>

Trigger the shutdown.

This will cause all ShutdownSignal and WrapCancel futures associated with this shutdown manager to be resolved.

The shutdown will not be considered complete until all DelayShutdownTokens are dropped.

If the shutdown was already started, this function returns an error.

Source

pub fn wrap_cancel<F: Future>(&self, future: F) -> WrapCancel<T, F>

Wrap a future so that it is cancelled (dropped) when the shutdown is triggered.

The returned future completes with Err(shutdown_reason) if the shutdown is triggered, and with Ok(x) if the wrapped future completes first.

Source

pub fn wrap_trigger_shutdown<F: Future>( &self, shutdown_reason: T, future: F, ) -> WrapTriggerShutdown<T, F>

Wrap a future to cause a shutdown when the future completes or when it is dropped.

Source

pub fn wrap_delay_shutdown<F: Future>( &self, future: F, ) -> Result<WrapDelayShutdown<T, F>, ShutdownAlreadyCompleted<T>>

Wrap a future to delay shutdown completion until the wrapped future completes or until it is dropped.

The returned future transparently completes with the value of the wrapped future. However, the shutdown will not be considered complete until the wrapped future completes or is dropped.

If the shutdown has already completed, this function returns an error.

Source

pub fn delay_shutdown_token( &self, ) -> Result<DelayShutdownToken<T>, ShutdownAlreadyCompleted<T>>

Get a token that delays shutdown completion as long as it exists.

The manager keeps track of all the tokens it hands out. The tokens can be cloned and sent to different threads and tasks. All tokens (including the clones) must be dropped before the shutdown is considered to be complete.

If the shutdown has already completed, this function returns an error.

If you want to delay the shutdown until a future completes, consider using Self::wrap_delay_shutdown() instead.

Source

pub fn trigger_shutdown_token( &self, shutdown_reason: T, ) -> TriggerShutdownToken<T>

Get a token that triggers a shutdown when dropped.

When a TriggerShutdownToken is dropped, the shutdown is triggered automatically. This applies to any token. If you clone a token five times and drop one of them, it will trigger a shutdown/

You can also use Self::wrap_trigger_shutdown() to wrap a future so that a shutdown is triggered when the future completes or if it is dropped.

Trait Implementations§

Source§

impl<T: Clone + Clone> Clone for ShutdownManager<T>

Source§

fn clone(&self) -> ShutdownManager<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Clone> Default for ShutdownManager<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for ShutdownManager<T>

§

impl<T> RefUnwindSafe for ShutdownManager<T>

§

impl<T> Send for ShutdownManager<T>
where T: Send,

§

impl<T> Sync for ShutdownManager<T>
where T: Send,

§

impl<T> Unpin for ShutdownManager<T>

§

impl<T> UnwindSafe for ShutdownManager<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.