Trait ResultExt

Source
pub trait ResultExt<T, E>: ResultSealed + Sized {
    // Required methods
    fn context<M>(self, msg: M) -> Result<T, Context<M, E>>
       where M: Display;
    fn with_context<F, M>(self, f: F) -> Result<T, Context<M, E>>
       where F: FnOnce(&E) -> M,
             M: Display;
}
Expand description

Extension traits for results.

This provides method to enrich the error in the result with additional context. See the general principles and examples at the crate level documentation.

Usually, this trait isn’t imported directly, but through the prelude.

Required Methods§

Source

fn context<M>(self, msg: M) -> Result<T, Context<M, E>>
where M: Display,

Wraps the error in another layer of context.

If the result is success, this does nothing. If it is error, it wraps the error in another layer, in the same way as calling .context on that error itself would.

If the construction of the message is expensive, consider using with_context.

Source

fn with_context<F, M>(self, f: F) -> Result<T, Context<M, E>>
where F: FnOnce(&E) -> M, M: Display,

Wraps the error in another layer of context.

This works in a similar way as context. However, the closure to construct the context is called only in case the result is Err, avoiding the construction cost in the success case.

As the common usage goes, string literal can be passed directly with context, but calling format! to construct the message would be better done with this method.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, E: Error> ResultExt<T, E> for Result<T, E>

Source§

fn context<M>(self, msg: M) -> Result<T, Context<M, E>>
where M: Display,

Source§

fn with_context<F, M>(self, f: F) -> Result<T, Context<M, E>>
where F: FnOnce(&E) -> M, M: Display,

Implementors§