pub struct Stopper { /* private fields */ }Expand description
A cancellation primitive with unified clone semantics.
This is the recommended default for most use cases. Clone it to share the cancellation state - any clone can cancel or check status.
Converts to StopToken via From/Into with zero
overhead — the existing Arc is reused, not double-wrapped.
§Example
use almost_enough::{Stopper, Stop};
let stop = Stopper::new();
// Pass a clone to another thread
let stop2 = stop.clone();
std::thread::spawn(move || {
while !stop2.should_stop() {
// do work
break;
}
}).join().unwrap();
// Cancel from original
stop.cancel();§Performance
- Size: 8 bytes (one pointer)
check(): ~1-2ns (single atomic load with Relaxed ordering)clone(): atomic incrementcancel(): atomic storeinto() -> StopToken: zero-cost (Arc pointer widening)
Implementations§
Source§impl Stopper
impl Stopper
Sourcepub fn cancelled() -> Self
pub fn cancelled() -> Self
Create a stopper that is already cancelled.
Useful for testing or when you want to signal immediate stop.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Signal all clones to stop.
A Release swap: writes made before a cancel() are visible to a
thread whose is_cancelled returns true after
it, even when several threads cancel. Calling it again changes nothing
else.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Whether cancellation has been requested, as an Acquire load.
When it returns true, this thread also sees every write the
cancelling thread made before cancel, such as a
reason code kept in an atomic. Call it after the work reports the
stop. In a loop, use check or
should_stop: one Relaxed load each, which see
the stop just as soon but promise nothing about other writes.
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.
Auto Trait Implementations§
impl Freeze for Stopper
impl RefUnwindSafe for Stopper
impl Send for Stopper
impl Sync for Stopper
impl Unpin for Stopper
impl UnsafeUnpin for Stopper
impl UnwindSafe for Stopper
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<C> StopDropRoll for Cwhere
C: Cancellable,
impl<C> StopDropRoll for Cwhere
C: Cancellable,
Source§fn stop_on_drop(&self) -> CancelGuard<C>
fn stop_on_drop(&self) -> CancelGuard<C>
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.