AsyncResultExt

Trait AsyncResultExt 

Source
pub trait AsyncResultExt<T, E> {
    // Required methods
    fn async_map<U, F, Fut>(self, op: F) -> impl Future<Output = Result<U, E>>
       where F: FnOnce(T) -> Fut,
             Fut: Future<Output = U>;
    fn async_and_then<U, F, Fut>(
        self,
        op: F,
    ) -> impl Future<Output = Result<U, E>>
       where F: FnOnce(T) -> Fut,
             Fut: Future<Output = Result<U, E>>;
    fn async_map_or<U, F, Fut>(
        self,
        default: U,
        op: F,
    ) -> impl Future<Output = U>
       where F: FnOnce(T) -> Fut,
             Fut: Future<Output = U>;
    fn async_map_or_else<U, D, F, Fut, DefFut>(
        self,
        default: D,
        op: F,
    ) -> impl Future<Output = U>
       where D: FnOnce(E) -> DefFut,
             F: FnOnce(T) -> Fut,
             DefFut: Future<Output = U>,
             Fut: Future<Output = U>;
    fn async_map_err<F, Fut, O>(
        self,
        op: F,
    ) -> impl Future<Output = Result<T, O>>
       where F: FnOnce(E) -> Fut,
             Fut: Future<Output = O>;
    fn async_inspect<F, Fut>(self, op: F) -> impl Future<Output = Self>
       where F: FnOnce(&T) -> Fut,
             Fut: Future<Output = ()>;
    fn async_inspect_err<F, Fut>(self, op: F) -> impl Future<Output = Self>
       where F: FnOnce(&E) -> Fut,
             Fut: Future<Output = ()>;
}
Expand description

Asynchronous extensions for Result<T, E>.

This trait provides async counterparts of common Result methods (map, and_then, map_err, inspect) that accept asynchronous closures.

Example:

use async_result_ext::AsyncResultExt;

#[tokio::main]
async fn main() {
    let r: Result<i32, &str> = Ok(2);

    let doubled = r.async_map(|v| async move { v * 2 }).await;
    assert_eq!(doubled, Ok(4));
}

Required Methods§

Source

fn async_map<U, F, Fut>(self, op: F) -> impl Future<Output = Result<U, E>>
where F: FnOnce(T) -> Fut, Fut: Future<Output = U>,

Asynchronous version of Result::map.

Applies an async function op to the Ok value. If the result is Err, it is returned unchanged.

use async_result_ext::AsyncResultExt;

let r: Result<i32, &str> = Ok(5);
let res = r.async_map(|v| async move { v + 1 }).await;
assert_eq!(res, Ok(6));
Source

fn async_and_then<U, F, Fut>(self, op: F) -> impl Future<Output = Result<U, E>>
where F: FnOnce(T) -> Fut, Fut: Future<Output = Result<U, E>>,

Asynchronous version of Result::and_then.

Chains async computations that return Result.

use async_result_ext::AsyncResultExt;

let r: Result<i32, &str> = Ok(2);
let res = r.async_and_then(|v| async move { Ok(v * 3) }).await;
assert_eq!(res, Ok(6));
Source

fn async_map_or<U, F, Fut>(self, default: U, op: F) -> impl Future<Output = U>
where F: FnOnce(T) -> Fut, Fut: Future<Output = U>,

Asynchronous version of Result::map_or.

If the result is Ok, applies async function op. If Err, returns the provided default value.

Source

fn async_map_or_else<U, D, F, Fut, DefFut>( self, default: D, op: F, ) -> impl Future<Output = U>
where D: FnOnce(E) -> DefFut, F: FnOnce(T) -> Fut, DefFut: Future<Output = U>, Fut: Future<Output = U>,

Asynchronous version of Result::map_or_else.

If the result is Ok, applies async function op. If Err, computes an async fallback via default.

Source

fn async_map_err<F, Fut, O>(self, op: F) -> impl Future<Output = Result<T, O>>
where F: FnOnce(E) -> Fut, Fut: Future<Output = O>,

Asynchronous version of Result::map_err.

Transforms the error using an async function op.

Source

fn async_inspect<F, Fut>(self, op: F) -> impl Future<Output = Self>
where F: FnOnce(&T) -> Fut, Fut: Future<Output = ()>,

Asynchronous version of Result::inspect.

Lets you asynchronously “peek” into the Ok value without modifying it.

Source

fn async_inspect_err<F, Fut>(self, op: F) -> impl Future<Output = Self>
where F: FnOnce(&E) -> Fut, Fut: Future<Output = ()>,

Asynchronous version of Result::inspect_err.

Lets you asynchronously “peek” into the Err value without modifying it.

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

Source§

async fn async_map<U, F, Fut>(self, op: F) -> Result<U, E>
where F: FnOnce(T) -> Fut, Fut: Future<Output = U>,

Source§

async fn async_and_then<U, F, Fut>(self, op: F) -> Result<U, E>
where F: FnOnce(T) -> Fut, Fut: Future<Output = Result<U, E>>,

Source§

async fn async_map_or<U, F, Fut>(self, default: U, op: F) -> U
where F: FnOnce(T) -> Fut, Fut: Future<Output = U>,

Source§

async fn async_map_or_else<U, D, F, Fut, DefFut>(self, default: D, op: F) -> U
where D: FnOnce(E) -> DefFut, F: FnOnce(T) -> Fut, DefFut: Future<Output = U>, Fut: Future<Output = U>,

Source§

async fn async_map_err<F, Fut, O>(self, op: F) -> Result<T, O>
where F: FnOnce(E) -> Fut, Fut: Future<Output = O>,

Source§

async fn async_inspect<F, Fut>(self, op: F) -> Self
where F: FnOnce(&T) -> Fut, Fut: Future<Output = ()>,

Source§

async fn async_inspect_err<F, Fut>(self, op: F) -> Self
where F: FnOnce(&E) -> Fut, Fut: Future<Output = ()>,

Implementors§