futures-async-stream 0.1.0-alpha.1

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

futures-async-stream

Build Status Crates.io Docs.rs License Minimum supported Rust version

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.

Usage

Add this to your Cargo.toml:

[dependencies]
futures-async-stream = "0.1.0-alpha.1"
futures-preview = "0.3.0-alpha.17"

The current futures-async-stream requires Rust nightly 2019-07-02 or later.

#[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` is built into `async_stream`. If you use `for_await` only in `async_stream`, there is no need to import `for_await`.
    #[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.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.