enough
Minimal cooperative cancellation trait for Rust.
A no_std, zero-dependency trait for cooperative cancellation.
The Trait
One required method. Option<T: Stop> implements Stop: None is
a no-op, Some delegates — enabling the may_stop() optimization
pattern (see below).
Quick Start
Accept impl Stop + 'static in your public API. Use
StopToken
from almost-enough internally — it handles the Unstoppable optimization
automatically and is the fastest option for real stop types:
use Stop;
use StopToken;
// Callers:
// decode(&data, Unstoppable)?; // no cancellation — zero cost
// decode(&data, stopper)?; // with cancellation
StopToken is Clone (Arc increment) for thread fan-out.
Stopper/SyncStopper convert to StopToken at zero cost via Into
(same Arc, no double-wrapping). Benchmarks show StopToken within 3%
of fully-inlined generic for Unstoppable, and 25% faster than generic
for Stopper.
Without almost-enough
Use &dyn Stop with may_stop().then_some():
Embedded / no_std
Use impl Stop (without 'static) to accept borrowed types like
StopRef<'a>:
Crate Structure
| Crate | Purpose |
|---|---|
enough |
Core trait: Stop, StopReason, Unstoppable |
almost-enough |
All implementations: Stopper, StopToken, StopSource, timeouts, combinators |
enough-ffi |
C FFI for cross-language use |
enough-tokio |
Bridge to tokio's CancellationToken |
Features
- None (default) -
no_stdcore:Stoptrait,StopReason,Unstoppable alloc- AddsBox<T>andArc<T>blanket impls forStopstd- Impliesalloc(kept for downstream compatibility)
License
MIT OR Apache-2.0