• Stars
    star
    167
  • Rank 226,635 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Async stream for Rust and the futures crate.

futures-async-stream

crates.io docs.rs license rustc build status

Async stream for Rust and the futures crate.

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

Usage

Add this to your Cargo.toml:

[dependencies]
futures-async-stream = "0.2"
futures = "0.3"

Compiler support: requires rustc nightly-2021-10-11+

#[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(proc_macro_hygiene, stmt_expr_attributes)]

use futures::stream::Stream;
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, #[stream] functions and stream_block! macros.

#[stream]

Creates streams via generators.

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

#![feature(generators)]

use futures::stream::Stream;
use futures_async_stream::stream;

// Returns a stream of i32
#[stream(item = i32)]
async fn foo(stream: impl Stream<Item = String>) {
    // `for_await` is built into `stream`. If you use `for_await` only in `stream`, there is no need to import `for_await`.
    #[for_await]
    for x in stream {
        yield x.parse().unwrap();
    }
}

To early exit from a #[stream] function or block, use return.

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

#[stream] can also be used on async blocks:

#![feature(generators, proc_macro_hygiene, stmt_expr_attributes)]

use futures::stream::Stream;
use futures_async_stream::stream;

fn foo() -> impl Stream<Item = i32> {
    #[stream]
    async move {
        for i in 0..10 {
            yield i;
        }
    }
}

Note that #[stream] on async block does not require the item argument, but it may require additional type annotations.

Using async stream functions in traits

You can use async stream functions in traits by passing boxed or boxed_local as an argument.

#![feature(generators)]

use futures_async_stream::stream;

trait Foo {
    #[stream(boxed, item = u32)]
    async fn method(&mut self);
}

struct Bar(u32);

impl Foo for Bar {
    #[stream(boxed, item = u32)]
    async fn method(&mut self) {
        while self.0 < u32::MAX {
            self.0 += 1;
            yield self.0;
        }
    }
}

A async stream function that received a boxed argument is converted to a function that returns Pin<Box<dyn Stream<Item = item> + Send + 'lifetime>>. If you passed boxed_local instead of boxed, async stream function returns a non-threadsafe stream (Pin<Box<dyn Stream<Item = item> + 'lifetime>>).

#![feature(generators)]

use std::pin::Pin;

use futures::stream::Stream;
use futures_async_stream::stream;

// The trait itself can be defined without unstable features.
trait Foo {
    fn method(&mut self) -> Pin<Box<dyn Stream<Item = u32> + Send + '_>>;
}

struct Bar(u32);

impl Foo for Bar {
    #[stream(boxed, item = u32)]
    async fn method(&mut self) {
        while self.0 < u32::MAX {
            self.0 += 1;
            yield self.0;
        }
    }
}

#[try_stream]

? operator can be used with the #[try_stream]. The Item of the returned stream is Result with Ok being the value yielded and Err the error type returned by ? operator or return Err(...).

#![feature(generators)]

use futures::stream::Stream;
use futures_async_stream::try_stream;

#[try_stream(ok = i32, error = Box<dyn std::error::Error>)]
async fn foo(stream: impl Stream<Item = String>) {
    #[for_await]
    for x in stream {
        yield x.parse()?;
    }
}

#[try_stream] can be used wherever #[stream] can be used.

To early exit from a #[try_stream] function or block, use return Ok(()).

How to write the equivalent code without this API?

#[for_await]

You can write this by combining while let loop, .await, pin! macro, and StreamExt::next() method:

use std::pin::pin;

use futures::stream::{Stream, StreamExt};

async fn collect(stream: impl Stream<Item = i32>) -> Vec<i32> {
    let mut vec = Vec::new();
    let mut stream = pin!(stream);
    while let Some(value) = stream.next().await {
        vec.push(value);
    }
    vec
}

#[stream]

You can write this by manually implementing the combinator:

use std::{
    pin::Pin,
    task::{ready, Context, Poll},
};

use futures::stream::Stream;
use pin_project::pin_project;

fn foo<S>(stream: S) -> impl Stream<Item = i32>
where
    S: Stream<Item = String>,
{
    Foo { stream }
}

#[pin_project]
struct Foo<S> {
    #[pin]
    stream: S,
}

impl<S> Stream for Foo<S>
where
    S: Stream<Item = String>,
{
    type Item = i32;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        if let Some(x) = ready!(self.project().stream.poll_next(cx)) {
            Poll::Ready(Some(x.parse().unwrap()))
        } else {
            Poll::Ready(None)
        }
    }
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

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.

More Repositories

1

cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Rust
741
star
2

pin-project

A crate for safe and ergonomic pin-projection.
Rust
447
star
3

cargo-hack

Cargo subcommand to provide various options useful for testing and continuous integration.
Rust
379
star
4

auto_enums

A library for to allow multiple return types by automatically generated enum.
Rust
303
star
5

install-action

GitHub Action for installing development tools (mainly from GitHub Releases).
Shell
256
star
6

upload-rust-binary-action

GitHub Action for building and uploading Rust binary to GitHub Releases.
Shell
163
star
7

pin-project-lite

A lightweight version of pin-project written with declarative macros.
Rust
135
star
8

portable-atomic

Portable atomic types including support for 128-bit atomics, atomic float, etc.
Rust
96
star
9

create-gh-release-action

GitHub Action for creating GitHub Releases based on changelog.
Shell
60
star
10

parse-changelog

Simple changelog parser, written in Rust.
Rust
46
star
11

replace-await

Migration tool for replacing await! macro with await syntax.
Rust
41
star
12

cargo-minimal-versions

Cargo subcommand for proper use of -Z minimal-versions and -Z direct-minimal-versions.
Rust
40
star
13

easy-ext

A lightweight attribute macro for easily writing extension trait pattern.
Rust
36
star
14

derive_utils

A procedural macro helper for easily writing custom derives for enums.
Rust
25
star
15

const_fn

A lightweight attribute for easy generation of const functions with conditional compilations.
Rust
23
star
16

atomic-memcpy

Byte-wise atomic memcpy.
Rust
21
star
17

setup-cross-toolchain-action

GitHub Action for setup toolchains for cross compilation and cross testing for Rust.
Shell
21
star
18

syn-serde

Library to serialize and deserialize Syn syntax trees.
Rust
15
star
19

futures-enum

#[derive(Future, Stream, Sink, AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead)] for enums.
Rust
13
star
20

atomic-maybe-uninit

Atomic operations on potentially uninitialized integers.
Rust
13
star
21

easytime

Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations.
Rust
13
star
22

rust-cross-toolchain

Toolchains for cross compilation and cross testing for Rust.
Shell
12
star
23

cargo-no-dev-deps

Cargo subcommand for running cargo without dev-dependencies.
Rust
11
star
24

cargo-config2

Load and resolve Cargo configuration.
Rust
10
star
25

iter-enum

#[derive(Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, Extend)] for enums.
Rust
9
star
26

negative-impl

Negative trait implementations on stable Rust.
Shell
9
star
27

coverage-helper

Helper for https://github.com/taiki-e/cargo-llvm-cov/issues/123.
Shell
6
star
28

find-crate

Find the crate name from the current Cargo.toml.
Rust
5
star
29

syn-mid

Providing the features between "full" and "derive" of syn.
Rust
5
star
30

assert-unmoved

A type that asserts that the underlying type is not moved after being pinned and mutably accessed.
Rust
4
star
31

cache-cargo-install-action

GitHub Action for `cargo install` with cache.
Shell
4
star
32

io-enum

#[derive(Read, Write, Seek, BufRead)] for enums.
Shell
4
star
33

semihosting

Semihosting for AArch64, ARM, RISC-V, MIPS, and MIPS64
Rust
3
star
34

target-spec-json

Structured access to rustc --print target-spec-json and --print all-target-specs-json.
Rust
2
star
35

build-context

Make build environment/target information available as constants in normal libraries and binaries.
Shell
2
star
36

github-actions

Shell
2
star
37

iced_style_config

Create Iced style sheets from configuration files.
Rust
2
star
38

dependabot-config

Structured access to the Dependabot configuration file.
Rust
2
star
39

taiki-e

1
star
40

workflows

Shell
1
star
41

checkout-action

GitHub Action for checking out a repository. (Simplified actions/checkout alternative without depending on Node.js.)
Shell
1
star
42

test

Shell
1
star
43

dockerfiles

Shell
1
star