Struct StopGuard

Source
pub struct StopGuard { /* private fields */ }
Expand description

Guard allowing to stop blocking operations when dropped.

§Usage

  1. Instantiate a guard and the paired StopToken via Self::new() in a future.
  2. Leave the guard as-is inside the future.
  3. Send the token to a blocking Tokio task (i.e., a tokio::task::spawn_blocking() closure).
  4. Check the token state periodically via StopToken::should_stop() in the blocking task; stop the task if it is true.

§Why this works

If a future is dropped (e.g., by timing it out, selecting between it and another future, etc.), blocking task(s) spawned inside the future will continue executing indefinitely because Rust / Tokio don’t provide means to stop threads (which means do exist in some OSes, but are notoriously unsafe to use). StopGuard provides an app-level solution to this concern. It is a thin non-cloneable wrapper around an Arc, and StopToken is a (cloneable) Weak reference to it. StopToken::should_stop() checks whether the strong count for the Arc is zero.

Implementations§

Source§

impl StopGuard

Source

pub fn new() -> (Self, StopToken)

Trait Implementations§

Source§

impl Debug for StopGuard

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T