Skip to main content

AsStopReason

Trait AsStopReason 

Source
pub trait AsStopReason {
    // Required method
    fn as_stop_reason(&self) -> Option<StopReason>;
}
Expand description

The StopReason an error represents, if it represents one.

An operation that was cancelled or timed out usually returns an error that wraps the StopReason it got from Stop::check. Callers use this trait to tell that apart from a real failure without knowing the error type: to skip a retry, log quietly, or record a progress phase as cancelled. StopReason implements it; an error type with a variant that wraps a StopReason implements it next to its From<StopReason>:

use enough::{AsStopReason, StopReason};

enum MyError {
    Corrupt,
    Stopped(StopReason),
}
impl From<StopReason> for MyError {
    fn from(reason: StopReason) -> Self {
        Self::Stopped(reason)
    }
}
impl AsStopReason for MyError {
    fn as_stop_reason(&self) -> Option<StopReason> {
        match self {
            Self::Stopped(reason) => Some(*reason),
            _ => None,
        }
    }
}
assert_eq!(
    MyError::Stopped(StopReason::TimedOut).as_stop_reason(),
    Some(StopReason::TimedOut)
);
assert!(MyError::Corrupt.as_stop_reason().is_none());

Required Methods§

Source

fn as_stop_reason(&self) -> Option<StopReason>

The StopReason this error represents, or None for any other failure.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§