Skip to main content

OptionExt

Trait OptionExt 

Source
pub trait OptionExt: Sealed {
    type Error: Error;

    // Required methods
    fn capture<E, K>(error_kind: K, option: Option<E>) -> Self
       where E: Context,
             K: ErrorKind + Into<<Self::Error as Error>::Kind>,
             Self::Error: TrivialError;
    fn convert<E, K>(error_kind: K, option: Option<E>) -> Self
       where E: Context,
             K: ErrorKind + Into<<Self::Error as Error>::Kind>,
             Self::Error: TrivialError;
    fn with_context<C, F>(self, context: F) -> Self
       where C: Context,
             F: FnOnce() -> C;
}
Expand description

Extension functions for Option<T> when T is an Error.

Required Associated Types§

Source

type Error: Error

The error type associated with this option (Some(_)).

Required Methods§

Source

fn capture<E, K>(error_kind: K, option: Option<E>) -> Self
where E: Context, K: ErrorKind + Into<<Self::Error as Error>::Kind>, Self::Error: TrivialError,

Convert an option from some foreign error, capturing the error context.

This is equivalent to the following code:

match option {
    Some(error) => Some(
        TargetError::new(error_kind.into()).with_context(|| error)
    ),
    None => None,
}
Source

fn convert<E, K>(error_kind: K, option: Option<E>) -> Self
where E: Context, K: ErrorKind + Into<<Self::Error as Error>::Kind>, Self::Error: TrivialError,

Convert an option from some foreign error.

This is equivalent to the following code:

match option {
    Some(error) => Some(TargetError::new(error_kind.into())),
    None => None,
}
Source

fn with_context<C, F>(self, context: F) -> Self
where C: Context, F: FnOnce() -> C,

Provide human-readable information about why an error occurred.

This is a wrapper which redirects to ErrorContext::with_context for the wrapped Some(value). If the Option is None, this information is discarded.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl<E> OptionExt for Option<E>
where E: Error,

Source§

type Error = E

Source§

fn capture<E2, K>(error_kind: K, option: Option<E2>) -> Self
where E2: Context, K: ErrorKind + Into<<Self::Error as Error>::Kind>, Self::Error: TrivialError,

Source§

fn convert<E2, K>(error_kind: K, option: Option<E2>) -> Self
where E2: Context, K: ErrorKind + Into<<Self::Error as Error>::Kind>, Self::Error: TrivialError,

Source§

fn with_context<C, F>(self, context: F) -> Self
where C: Context, F: FnOnce() -> C,

Implementors§