enough 0.2.0

Minimal cooperative cancellation trait for long-running operations
Documentation

enough

Minimal cooperative cancellation trait for Rust.

Crates.io Documentation License MSRV

A minimal, no_std trait for cooperative cancellation. Zero dependencies.

StopReason is 1 byte and check() compiles to a single boolean read from the stack.

For Library Authors

Accept impl Stop in your functions:

use enough::{Stop, StopReason};

pub fn decode(data: &[u8], stop: impl Stop) -> Result<Vec<u8>, MyError> {
    for (i, chunk) in data.chunks(1024).enumerate() {
        if i % 16 == 0 {
            stop.check()?; // Returns Err(StopReason) if stopped
        }
        // process...
    }
    Ok(vec![])
}

impl From<StopReason> for MyError {
    fn from(r: StopReason) -> Self { MyError::Stopped(r) }
}

Zero-Cost Default

use enough::Never;

// Compiles away completely - zero runtime cost
let result = my_lib::decode(&data, Never);

What's in This Crate

This crate provides only the core trait and types:

  • Stop - The cooperative cancellation trait
  • StopReason - Why an operation stopped (Cancelled or TimedOut)
  • Never - Zero-cost "never stop" implementation

For concrete cancellation implementations (Stopper, StopSource, timeouts, etc.), see almost-enough.

Features

  • None (default) - no_std core: Stop trait, StopReason, Never
  • alloc - Adds Box<T> and Arc<T> blanket impls for Stop
  • std - Implies alloc. Adds std::error::Error impl for StopReason

See Also

  • almost-enough - All implementations: Stopper, StopSource, ChildStopper, timeouts, combinators, guards
  • enough-ffi - FFI helpers for C#, Python, Node.js
  • enough-tokio - Tokio CancellationToken bridge

License

MIT OR Apache-2.0