pub struct StopToken { /* private fields */ }Expand description
A shared-ownership Stop implementation with cheap Clone.
Wraps any Stop in an Arc for shared ownership across threads.
Cloning is an atomic increment — no heap allocation.
§Indirection Collapsing
StopToken::new() detects when you pass another StopToken and unwraps
it instead of double-wrapping. No-op stops (Unstoppable) are stored
as None — check() short-circuits without any vtable dispatch.
§Example
use almost_enough::{StopToken, Stopper, Stop, StopReason};
let stopper = Stopper::new();
let stop = StopToken::new(stopper.clone());
let stop2 = stop.clone(); // cheap Arc clone
stopper.cancel();
assert!(stop.should_stop());
assert!(stop2.should_stop()); // both see cancellationImplementations§
Source§impl StopToken
impl StopToken
Sourcepub fn new<T: Stop + 'static>(stop: T) -> Self
pub fn new<T: Stop + 'static>(stop: T) -> Self
Create a new StopToken from any Stop implementation.
If stop is already a StopToken, it is unwrapped instead of
double-wrapping (no extra indirection).
Sourcepub fn from_arc<T: Stop + 'static>(arc: Arc<T>) -> Self
pub fn from_arc<T: Stop + 'static>(arc: Arc<T>) -> Self
Create a StopToken from an existing Arc<T> without re-wrapping.
This is zero-cost — just widens the pointer. Use this when you
already have an Arc-wrapped stop type.
If T is StopToken, the inner Arc is extracted to avoid double
indirection (Arc<Arc<dyn Stop>>).
use almost_enough::{StopToken, Stopper, Stop};
use std::sync::Arc;
let stopper = Arc::new(Stopper::new());
let stop = StopToken::from_arc(stopper); // pointer widening, no allocation
assert!(!stop.should_stop());Trait Implementations§
Source§impl From<Stopper> for StopToken
Zero-cost conversion: reuses the Stopper’s existing Arc via pointer widening.
No double-wrapping — the resulting StopToken shares the same heap allocation.
impl From<Stopper> for StopToken
Zero-cost conversion: reuses the Stopper’s existing Arc via pointer widening.
No double-wrapping — the resulting StopToken shares the same heap allocation.
Source§impl From<SyncStopper> for StopToken
Zero-cost conversion: reuses the SyncStopper’s existing Arc via pointer widening.
impl From<SyncStopper> for StopToken
Zero-cost conversion: reuses the SyncStopper’s existing Arc via pointer widening.