StopDropRoll

Trait StopDropRoll 

Source
pub trait StopDropRoll: Cancellable {
    // Required method
    fn stop_on_drop(&self) -> CancelGuard<Self>;
}
Expand description

Extension trait for creating CancelGuards.

This trait is implemented for types that support cancellation, allowing you to create RAII guards that stop on drop.

§Supported Types

§Example

use almost_enough::{Stopper, StopDropRoll};

fn fallible_work(source: &Stopper) -> Result<i32, &'static str> {
    let guard = source.stop_on_drop();

    // If we return Err or panic, source is stopped
    let result = compute()?;

    // Success - don't stop
    guard.disarm();
    Ok(result)
}

fn compute() -> Result<i32, &'static str> {
    Ok(42)
}

let source = Stopper::new();
assert_eq!(fallible_work(&source), Ok(42));
assert!(!source.is_cancelled());

§With ChildStopper

use almost_enough::{Stopper, ChildStopper, StopDropRoll, Stop, StopExt};

let parent = Stopper::new();
let child = parent.child();

{
    let guard = child.stop_on_drop();
    // guard dropped, child is stopped
}

assert!(child.is_cancelled());
assert!(!parent.is_cancelled()); // Parent is NOT affected

Required Methods§

Source

fn stop_on_drop(&self) -> CancelGuard<Self>

Create a guard that will stop this source on drop.

The guard can be disarmed via CancelGuard::disarm() to prevent stopping.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§