Skip to main content

AsyncResultTools

Trait AsyncResultTools 

Source
pub trait AsyncResultTools<T, E> {
    // Required methods
    fn is_ok_and_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
       where F: FnOnce(T) -> Fut,
             Fut: Future<Output = bool>;
    fn map_async<B, F, Fut>(self, f: F) -> impl Future<Output = Result<B, E>>
       where F: FnOnce(T) -> Fut,
             Fut: Future<Output = B>;
    fn and_then_async<B, F, Fut>(
        self,
        f: F,
    ) -> impl Future<Output = Result<B, E>>
       where F: FnOnce(T) -> Fut,
             Fut: Future<Output = Result<B, E>>;
    fn map_err_async<F, Fut, E2>(
        self,
        f: F,
    ) -> impl Future<Output = Result<T, E2>>
       where F: FnOnce(E) -> Fut,
             Fut: Future<Output = E2>;
    fn or_else_async<F, Fut>(self, f: F) -> impl Future<Output = Result<T, E>>
       where F: FnOnce(E) -> Fut,
             Fut: Future<Output = Result<T, E>>;
}
Expand description

An extension trait for Result<T, E> providing asynchronous combinators.

Required Methods§

Source

fn is_ok_and_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
where F: FnOnce(T) -> Fut, Fut: Future<Output = bool>,

Asynchronously evaluates whether the result is Ok and satisfies a given predicate.

If the result is Ok, the provided asynchronous predicate f is applied to the inner value. Returns true if the predicate resolves to true, otherwise returns false. If the result is Err, returns false without calling the predicate.

§Example
use async_iter_ext::AsyncResultTools;
use async_std::task::block_on;

async fn check_even(n: u32) -> bool {
  n % 2 == 0
}

let res: Result<u32, ()> = Ok(4);
assert!(block_on(async { res.is_ok_and_async(check_even).await }));
Source

fn map_async<B, F, Fut>(self, f: F) -> impl Future<Output = Result<B, E>>
where F: FnOnce(T) -> Fut, Fut: Future<Output = B>,

Applies an asynchronous transformation to the Ok value, if present.

If the result is Ok, the provided asynchronous function f is applied to the inner value, and the result is wrapped in Ok. If the result is Err, it is returned unchanged.

§Example
use async_iter_ext::AsyncResultTools;
use async_std::task::block_on;

async fn double(x: u32) -> u32 {
  x * 2
}

let res: Result<u32, &str> = Ok(3);
let doubled = block_on(async { res.map_async(double).await });
assert_eq!(doubled, Ok(6));
Source

fn and_then_async<B, F, Fut>(self, f: F) -> impl Future<Output = Result<B, E>>
where F: FnOnce(T) -> Fut, Fut: Future<Output = Result<B, E>>,

Applies an asynchronous function to the contained Ok value, returning a new Result.

Similar to Result::and_then, but with support for async functions.

Source

fn map_err_async<F, Fut, E2>(self, f: F) -> impl Future<Output = Result<T, E2>>
where F: FnOnce(E) -> Fut, Fut: Future<Output = E2>,

Applies an asynchronous function to the contained Err value, returning a new Result.

Similar to Result::map_err, but with async support.

Source

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

Applies an asynchronous fallback function if the result is an Err.

Similar to Result::or_else, but async.

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

Source§

async fn is_ok_and_async<F, Fut>(self, f: F) -> bool
where F: FnOnce(T) -> Fut, Fut: Future<Output = bool>,

Source§

async fn map_async<B, F, Fut>(self, f: F) -> Result<B, E>
where F: FnOnce(T) -> Fut, Fut: Future<Output = B>,

Source§

async fn and_then_async<B, F, Fut>(self, f: F) -> Result<B, E>
where F: FnOnce(T) -> Fut, Fut: Future<Output = Result<B, E>>,

Source§

async fn map_err_async<F, Fut, E2>(self, f: F) -> Result<T, E2>
where F: FnOnce(E) -> Fut, Fut: Future<Output = E2>,

Source§

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

Implementors§