pub trait Stop: Send + Sync {
// Required method
fn check(&self) -> Result<(), StopReason>;
// Provided method
fn should_stop(&self) -> bool { ... }
}Expand description
Cooperative cancellation check.
Implement this trait for custom cancellation sources. The implementation
must be thread-safe (Send + Sync) to support parallel processing and
async runtimes.
§Example Implementation
use enough::{Stop, StopReason};
use core::sync::atomic::{AtomicBool, Ordering};
pub struct MyStop<'a> {
cancelled: &'a AtomicBool,
}
impl Stop for MyStop<'_> {
fn check(&self) -> Result<(), StopReason> {
if self.cancelled.load(Ordering::Relaxed) {
Err(StopReason::Cancelled)
} else {
Ok(())
}
}
}Required Methods§
Sourcefn check(&self) -> Result<(), StopReason>
fn check(&self) -> Result<(), StopReason>
Check if the operation should stop.
Returns Ok(()) to continue, Err(StopReason) to stop.
Call this periodically in long-running loops. The frequency depends on your workload - typically every 16-1000 iterations is reasonable.
Provided Methods§
Sourcefn should_stop(&self) -> bool
fn should_stop(&self) -> bool
Returns true if the operation should stop.
Convenience method for when you want to handle stopping yourself
rather than using the ? operator.