Skip to main content

ResultExt

Trait ResultExt 

Source
pub trait ResultExt<T, E> {
    // Required methods
    fn context<C>(self, context: C) -> Result<T, ContextError<E, C>>;
    fn with_context<C, F>(self, f: F) -> Result<T, ContextError<E, C>>
       where F: FnOnce() -> C;
}
Expand description

Extension trait for Result types to add context to errors

anyhow::Context also adds a context method to Result. With both traits in scope, result.context(...) is ambiguous; import only one of them in a module, or call this one by path.

§Example

use error_forge::{AppError, ResultExt};

let result: Result<(), AppError> = Err(AppError::config("bad value"));
let wrapped = ResultExt::context(result, "loading settings");
assert!(wrapped.unwrap_err().to_string().starts_with("loading settings: "));

Required Methods§

Source

fn context<C>(self, context: C) -> Result<T, ContextError<E, C>>

Adds context to the error variant of the Result

Source

fn with_context<C, F>(self, f: F) -> Result<T, ContextError<E, C>>
where F: FnOnce() -> C,

Adds context to the error variant using a closure that is only called on error

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<T, E> for Result<T, E>

Source§

fn context<C>(self, context: C) -> Result<T, ContextError<E, C>>

Source§

fn with_context<C, F>(self, f: F) -> Result<T, ContextError<E, C>>
where F: FnOnce() -> C,

Implementors§