#error-chain #future #workaround #future-chain #out-of-box

futures-error-chain

A workaround until futures-rs and error-chain crates works out-of-box with each other

1 unstable release

Uses old Rust 2015

0.1.0 May 18, 2017

#6 in #workaround


Used in vault_client

MIT/Apache

5KB
88 lines

futures-error-chain

This library is a workaround until futures-rs and error-chain crates works out-of-box with each other

Usage

Just use like the error-chain crate, you run the future_chain! macro that will create the code needed to work with error-chain crate.

Example:

#![recursion_limit = "1024"]

#[macro_use]
extern crate error_chain;

extern crate futures;
extern crate futures_cpupool;

#[macro_use]
extern crate futures_error_chain;

mod foo {
    mod errors {
        error_chain! {
            errors {
                Foo(err: String) {
                    description("Foo error")
                    display("Foo error: {}", err)
                }
            }
        }

        future_chain!{}
    }

    pub use self::errors::*;

    use futures::future;

    fn bar() -> FutureChain<String> {
        future::err(ErrorKind::Foo("bar".to_owned()).into()).boxed()
    }

    fn bar2() -> FutureChain<String> {
        future::ok("bar2".to_owned()).boxed()
    }

    pub fn foo() -> FutureChain<String> {
        bar().and_then(|_| bar2()).chain_err(|| "foo")
    }
}

mod errors {
    error_chain! {
        links {
            Foo(::foo::Error, ::foo::ErrorKind);
        }
    }
}

use errors::*;

fn my_main() -> Result<()> {
    use futures_cpupool::CpuPool;
    use futures::Future;

    let pool = CpuPool::new_num_cpus();

    let f = foo::foo();
    let f2 = pool.spawn(f);

    f2.wait()?;

    Ok(())
}

quick_main!(my_main);

Dependencies

~53KB