futures_macro_await

Macro await_item

Source
macro_rules! await_item {
    ($e:expr) => { ... };
}
Expand description

Await an item from a stream inside an #[async] function.

You should pass an object implementing [Stream] to this macro, it will implicitly yield while that stream returns [Async::Pending] and evaluate to a Result containing the next item or error from that stream.

If you want to iterate over all items in a Stream you should instead see the documentation on #[async] for in the main #[async] documentation.

§Examples

#![feature(proc_macro, generators, pin)]
extern crate futures;

use futures::prelude::*;
use futures::stream;
use futures::stable::block_on_stable;

#[async]
fn eventually_ten() -> Result<u32, u32> {
    let mut stream = stream::repeat::<u32, u32>(5);
    if let Some(first) = await_item!(stream)? {
        if let Some(second) = await_item!(stream)? {
            return Ok(first + second);
        }
    }
    Err(0)
}

assert_eq!(Ok(10), block_on_stable(eventually_ten()));