pub trait StopExt: Stop + Sized {
// Provided methods
fn or<S: Stop>(self, other: S) -> OrStop<Self, S> { ... }
fn into_boxed(self) -> BoxedStop
where Self: 'static { ... }
fn into_token(self) -> StopToken
where Self: 'static { ... }
fn child(&self) -> ChildStopper
where Self: Clone + 'static { ... }
}Expand description
Extension trait providing ergonomic combinators for Stop implementations.
This trait is automatically implemented for all Stop + Sized types.
§Example
use almost_enough::{StopSource, Stop, StopExt};
let source_a = StopSource::new();
let source_b = StopSource::new();
// Combine with .or()
let combined = source_a.as_ref().or(source_b.as_ref());
assert!(!combined.should_stop());
source_b.cancel();
assert!(combined.should_stop());Provided Methods§
Sourcefn or<S: Stop>(self, other: S) -> OrStop<Self, S>
fn or<S: Stop>(self, other: S) -> OrStop<Self, S>
Combine this stop with another, stopping if either stops.
This is equivalent to OrStop::new(self, other) but with a more
ergonomic method syntax that allows chaining.
§Example
use almost_enough::{StopSource, Stop, StopExt};
let timeout = StopSource::new();
let cancel = StopSource::new();
let combined = timeout.as_ref().or(cancel.as_ref());
assert!(!combined.should_stop());
cancel.cancel();
assert!(combined.should_stop());§Chaining
Multiple sources can be chained:
use almost_enough::{StopSource, Stop, StopExt};
let a = StopSource::new();
let b = StopSource::new();
let c = StopSource::new();
let combined = a.as_ref().or(b.as_ref()).or(c.as_ref());
c.cancel();
assert!(combined.should_stop());Sourcefn into_boxed(self) -> BoxedStopwhere
Self: 'static,
👎Deprecated since 0.4.5: use into_token(), whose StopToken checks the same way and is Clone
fn into_boxed(self) -> BoxedStopwhere
Self: 'static,
use into_token(), whose StopToken checks the same way and is Clone
Convert this stop into a BoxedStop. Deprecated: use
into_token(), whose StopToken checks the
same way and is Clone.
Sourcefn into_token(self) -> StopTokenwhere
Self: 'static,
fn into_token(self) -> StopTokenwhere
Self: 'static,
Convert this stop into a shared, cloneable StopToken.
The result is Clone (via Arc increment) and can be sent across
threads. Use this when you need owned, cloneable type erasure.
If self is already a StopToken, this is a no-op (no double-wrapping).
§Example
use almost_enough::{Stopper, StopToken, Stop, StopExt};
let stop = Stopper::new();
let dyn_stop: StopToken = stop.into_token();
let clone = dyn_stop.clone(); // cheap Arc incrementSourcefn child(&self) -> ChildStopperwhere
Self: Clone + 'static,
fn child(&self) -> ChildStopperwhere
Self: Clone + 'static,
Create a child stop that inherits cancellation from this stop.
The returned ChildStopper will stop if:
- Its own
cancel()is called - This parent stop is cancelled
Cancelling the child does NOT affect the parent.
§Example
use almost_enough::{Stopper, Stop, StopExt};
let parent = Stopper::new();
let child = parent.child();
// Child cancellation is independent
child.cancel();
assert!(!parent.should_stop());
assert!(child.should_stop());
// Parent cancellation propagates
let child2 = parent.child();
parent.cancel();
assert!(child2.should_stop());Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".