[][src]Crate futures_async_stream

Async stream API experiment that may be introduced as a language feature in the future.

This crate provides useful features for streams, using unstable async_await and generators.

#[for_await]

Processes streams using a for loop.

This is a reimplement of futures-await's #[async] for loops for futures 0.3 and is an experimental implementation of the idea listed as the next step of async/await.

#![feature(async_await, stmt_expr_attributes, proc_macro_hygiene)]
use futures::prelude::*;
use futures_async_stream::for_await;

async fn collect(stream: impl Stream<Item = i32>) -> Vec<i32> {
    let mut vec = Vec::new();
    #[for_await]
    for value in stream {
        vec.push(value);
    }
    vec
}

value has the Item type of the stream passed in. Note that async for loops can only be used inside of async functions, closures, blocks, #[async_stream] functions and async_stream_block! macros.

#[async_stream]

Creates streams via generators.

This is a reimplement of futures-await's #[async_stream] for futures 0.3 and is an experimental implementation of the idea listed as the next step of async/await.

#![feature(async_await, generators)]
use futures::prelude::*;
use futures_async_stream::async_stream;

// Returns a stream of i32
#[async_stream(item = i32)]
async fn foo(stream: impl Stream<Item = String>) {
    #[for_await]
    for x in stream {
        yield x.parse().unwrap();
    }
}

#[async_stream] must have an item type specified via item = some::Path and the values output from the stream must be yielded via the yield expression.

Re-exports

pub use futures_async_stream_macro::for_await;
pub use futures_async_stream_macro::async_stream;
pub use futures_async_stream_macro::async_stream_block;