Skip to main content

AsyncResultExt

Trait AsyncResultExt 

Source
pub trait AsyncResultExt<T, E>: Future<Output = Result<T, E>> + Sized {
    // Provided method
    fn with_context<C>(self, ctx: C) -> WithContext<Self, E, C> 
       where C: FnOnce(&E) -> String { ... }
}
Expand description

Extension trait providing a .with_context() method for futures resolving to Result<T, E>.

This method allows attaching additional context to errors lazily, by supplying a closure that is only executed if the future resolves to an error.

§Example

some_async_fn()
    .with_context(|err| format!("Failed due to: {}", err))
    .await;

Provided Methods§

Source

fn with_context<C>(self, ctx: C) -> WithContext<Self, E, C>
where C: FnOnce(&E) -> String,

Adds context to an error produced by this future lazily.

The closure ctx is called only if the future resolves to an error, producing a string context to be attached to the error.

§Parameters
  • ctx: closure to create context string from error reference
§Returns

A future that resolves to Result<T, AsyncError<E>>, where errors are wrapped to include context.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl<T, E, Fut> AsyncResultExt<T, E> for Fut
where Fut: Future<Output = Result<T, E>> + Sized,