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 (or a
BoxedStop, which wraps one) and reuses it instead of
double-wrapping. No-op stops (Unstoppable) are stored as None —
check() short-circuits without any vtable dispatch. A Stopper is
checked as a direct atomic load, without a vtable. A SyncStopper keeps
its own Arc (no allocation) and is checked through the vtable.
§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.
use almost_enough::{StopToken, Stopper, Stop};
use std::sync::Arc;
let stopper = Arc::new(Stopper::new());
let stop = StopToken::from_arc(stopper);
assert!(!stop.should_stop());Trait Implementations§
Source§impl From<Stopper> for StopToken
Zero-cost conversion: reuses the Stopper’s Arc. Direct atomic dispatch, no vtable.
impl From<Stopper> for StopToken
Zero-cost conversion: reuses the Stopper’s Arc. Direct atomic dispatch, no vtable.
Source§impl From<SyncStopper> for StopToken
Reuses the SyncStopper’s Arc (no allocation); checks go through the vtable.
impl From<SyncStopper> for StopToken
Reuses the SyncStopper’s Arc (no allocation); checks go through the vtable.
Source§fn from(stopper: SyncStopper) -> Self
fn from(stopper: SyncStopper) -> Self
Auto Trait Implementations§
impl !RefUnwindSafe for StopToken
impl !UnwindSafe for StopToken
impl Freeze for StopToken
impl Send for StopToken
impl Sync for StopToken
impl Unpin for StopToken
impl UnsafeUnpin for StopToken
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneStop for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DebouncedTimeoutExt for Twhere
T: Stop,
impl<T> DebouncedTimeoutExt for Twhere
T: Stop,
Source§fn with_debounced_timeout(self, duration: Duration) -> DebouncedTimeout<Self>
fn with_debounced_timeout(self, duration: Duration) -> DebouncedTimeout<Self>
Source§fn with_debounced_deadline(self, deadline: Instant) -> DebouncedTimeout<Self>
fn with_debounced_deadline(self, deadline: Instant) -> DebouncedTimeout<Self>
Source§impl<T> StopExt for Twhere
T: Stop,
impl<T> StopExt for Twhere
T: Stop,
Source§fn or<S: Stop>(self, other: S) -> OrStop<Self, S>
fn or<S: Stop>(self, other: S) -> OrStop<Self, S>
Source§fn into_boxed(self) -> BoxedStopwhere
Self: 'static,
fn into_boxed(self) -> BoxedStopwhere
Self: 'static,
use into_token(), whose StopToken checks the same way and is Clone
BoxedStop. Deprecated: use
into_token(), whose StopToken checks the
same way and is Clone.