Skip to main content

ResultExt

Trait ResultExt 

Source
pub trait ResultExt: Sealed {
    type Error: Error;
    type Result: Sized;

    // Required methods
    fn capture<E, K>(error_kind: K, result: Result<Self::Result, E>) -> Self
       where E: Context,
             K: ErrorKind + Into<<Self::Error as Error>::Kind>,
             Self::Error: TrivialError;
    fn convert<E, K>(error_kind: K, result: Result<Self::Result, 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 Result<T, E> when E is an Error.

Required Associated Types§

Source

type Error: Error

The error type associated with this result (Err(_)).

Source

type Result: Sized

The result type associated with this result (Ok(_)).

Required Methods§

Source

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

Convert a result from some foreign error, capturing the error context.

This is equivalent to the following code:

match result {
    Ok(value) => Ok(value),
    Err(error) => Err(
        TargetError::new(error_kind.into()).with_context(|| error)
    ),
}
Source

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

Convert a result from some foreign error.

This is equivalent to the following code:

match result {
    Ok(value) => Ok(value),
    Err(_) => Err(TargetError::new(error_kind.into())),
}
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 Err(error). If the Result is Ok(_), 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<T, E> ResultExt for Result<T, E>
where E: Error,

Source§

type Error = E

Source§

type Result = T

Source§

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

Source§

fn convert<E2, K>(error_kind: K, result: Result<Self::Result, 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§