Macro futures_macro_await::await_item [] [src]

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

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.


This example is not tested
#![feature(proc_macro, generators, pin)]
extern crate futures;

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

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);

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