Crate enough

Crate enough 

Source
Expand description

§enough

Minimal cooperative cancellation trait for long-running operations.

This crate provides the core Stop trait that codec authors and library writers can use to support cancellation. It is no_std with zero dependencies.

§For Library Authors

Accept impl Stop in your long-running functions:

use enough::{Stop, StopReason};

pub fn decode(data: &[u8], stop: impl Stop) -> Result<Vec<u8>, DecodeError> {
    let mut output = Vec::new();
    for (i, chunk) in data.chunks(1024).enumerate() {
        // Check periodically in hot loops
        if i % 16 == 0 {
            stop.check()?;
        }
        // process chunk...
        output.extend_from_slice(chunk);
    }
    Ok(output)
}

#[derive(Debug)]
pub enum DecodeError {
    Stopped(StopReason),
    InvalidData,
}

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

§Zero-Cost When Not Needed

Use Never when you don’t need cancellation:

use enough::Never;

// Compiles to nothing - zero runtime cost
// let result = my_codec::decode(&data, Never);

§Implementations

This crate provides only the trait and a zero-cost Never implementation. For concrete cancellation primitives (Stopper, StopSource, timeouts, etc.), see the almost-enough crate.

§Feature Flags

  • None (default) - Core trait only, no_std compatible
  • std - Adds std::error::Error impl for StopReason

Structs§

Never
A Stop implementation that never stops.

Enums§

StopReason
Why an operation was stopped.

Traits§

Stop
Cooperative cancellation check.