#future #mapping #async #executor #block #u32 #ideomatic

future-result

An ideomatic way of mapping a Result to a Future of Result

2 unstable releases

0.1.0 Aug 8, 2020
0.0.0 Aug 8, 2020

#1659 in Asynchronous

37 downloads per month

MIT/Apache

7KB

future-result

A Rust crate that provides an ideomatic way of mapping a Result to a Future of Result.

Example usage

use future_result::FutureResult;

async fn add_1(x: u32) -> u32 {
    x + 1
}

fn main() {
    let fut = async {
        let ok: Result<u32, ()> = Ok(41);
        let ok = ok.then_map(add_1).await;

        assert_eq!(Ok(42), ok);

        // ...

        let err: Result<(), u32> = Err(9);
        let err = err.then_map_err(add_1).await;

        assert_eq!(Err(10), err);
    };

    futures::executor::block_on(fut);
}

lib.rs:

An ideomatic way of mapping a Result to a Future of Result.

Example usage

use future_result::FutureResult;

async fn add_1(x: u32) -> u32 {
    x + 1
}

fn main() {
    let fut = async {
        let ok: Result<u32, ()> = Ok(41);
        let ok = ok.then_map(add_1).await;

        assert_eq!(Ok(42), ok);

        // ...

        let err: Result<(), u32> = Err(9);
        let err = err.then_map_err(add_1).await;

        assert_eq!(Err(10), err);
    };

    futures::executor::block_on(fut);
}

Dependencies

~305–760KB
~18K SLoC