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 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,
fn into_boxed(self) -> BoxedStopwhere
Self: 'static,
Convert this stop into a boxed trait object.
This is useful for preventing monomorphization at API boundaries.
Instead of generating a new function for each impl Stop type,
you can erase the type to BoxedStop and have a single implementation.
§Example
use almost_enough::{Stopper, BoxedStop, Stop, StopExt};
// This function is monomorphized for each Stop type
fn process_generic(stop: impl Stop + 'static) {
// Erase type at boundary
process_concrete(stop.into_boxed());
}
// This function has only one implementation
fn process_concrete(stop: BoxedStop) {
while !stop.should_stop() {
break;
}
}
let stop = Stopper::new();
process_generic(stop);Sourcefn 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", so this trait is not object safe.