#![feature(async_await)]
use implicit_await::{defer, implicit_await};
use std::future::Future;
use futures::future::{join, ready, FutureExt};
use futures::executor::ThreadPool;
fn main() {
ThreadPool::new().unwrap().run(sum().inspect(|sum| match sum {
Ok(sum) => println!("The sum is {}", sum),
Err(_) => println!("Summation failed")
})).unwrap();
}
type IntResult = Result<u32, ()>;
#[implicit_await]
async fn sum() -> IntResult {
let one: u32 = num_sync(1)?;
let two: u32 = num_fut(2)?;
let three: u32 = num_async(3)?;
let (four, five, six) = defer! {(
num_fut(4),
num_async(5),
num_async(6)
)};
let (four, five) = join(four, five);
let six = six.await?;
Ok(one + two + three + four? + five? + six)
}
async fn num_async(num: u32) -> IntResult {
Ok(num)
}
fn num_fut(num: u32) -> impl Future<Output = IntResult> {
ready(Ok(num))
}
fn num_sync(num: u32) -> IntResult {
Ok(num)
}