CancelGuard

Struct CancelGuard 

Source
pub struct CancelGuard<C: Cancellable> { /* private fields */ }
Expand description

A guard that cancels a source when dropped, unless disarmed.

This provides RAII-style cancellation for cleanup on error paths or panics. Create one via the StopDropRoll trait.

§Example

use almost_enough::{Stopper, StopDropRoll};

let source = Stopper::new();

{
    let guard = source.stop_on_drop();
    // guard dropped here, source gets cancelled
}

assert!(source.is_cancelled());

§Disarming

Call disarm() to prevent cancellation:

use almost_enough::{Stopper, StopDropRoll};

let source = Stopper::new();

{
    let guard = source.stop_on_drop();
    guard.disarm(); // Prevents cancellation
}

assert!(!source.is_cancelled());

Implementations§

Source§

impl<C: Cancellable> CancelGuard<C>

Source

pub fn new(source: C) -> Self

Create a new guard that will cancel the source on drop.

Prefer using StopDropRoll::stop_on_drop() instead.

Source

pub fn disarm(self)

Disarm the guard, preventing cancellation on drop.

Call this when the guarded operation succeeds and you don’t want to cancel.

§Example
use almost_enough::{Stopper, StopDropRoll};

let source = Stopper::new();
let guard = source.stop_on_drop();

// Operation succeeded, don't cancel
guard.disarm();  // Consumes guard, preventing cancellation

assert!(!source.is_cancelled());
Source

pub fn is_armed(&self) -> bool

Check if this guard is still armed (will cancel on drop).

Source

pub fn source(&self) -> Option<&C>

Get a reference to the underlying source, if still armed.

Trait Implementations§

Source§

impl<C: Debug + Cancellable> Debug for CancelGuard<C>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<C: Cancellable> Drop for CancelGuard<C>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<C> Freeze for CancelGuard<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for CancelGuard<C>
where C: RefUnwindSafe,

§

impl<C> Send for CancelGuard<C>

§

impl<C> Sync for CancelGuard<C>
where C: Sync,

§

impl<C> Unpin for CancelGuard<C>
where C: Unpin,

§

impl<C> UnwindSafe for CancelGuard<C>
where C: UnwindSafe,

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.