百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

futures-async-stream

> 编程语言
开源

用于 Rust 和 futures crate 的异步流。

192 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于 Rust 和 futures crate 的异步流。

futures-async-stream

Async stream for Rust and the futures crate.

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

Usage

Add this to your Cargo.toml:

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

Compiler support: requires rustc nightly-2024-04-25+

#[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.

rust
#![feature(proc_macro_hygiene, stmt_expr_attributes)]

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

async fn collect(stream: impl Stream) -> Vec {
    let mut vec = vec![];
    #[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 coroutines.

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.

rust
#![feature(coroutines)]

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

// Returns a stream of i32
#[stream(item = i32)]
async fn foo(stream: impl Stream) {
    // `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:

rust
#![feature(coroutines, proc_macro_hygiene, stmt_expr_attributes)]

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

fn foo() -> impl Stream {
    #[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.

rust
#![feature(coroutines)]

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 >`.
If you passed `boxed_local` instead of `boxed`, async stream function
returns a non-thread-safe stream (`Pin + 'lifetime>>`).

```rust
#![feature(coroutines)]

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 + Send + '_>>;
}

struct Bar(u32);

impl Foo for Bar {
    #[stream(boxed, item = u32)]
    async fn method(&mut self) {
        while self.0 )]
async fn foo(stream: impl Stream) {
    #[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:

rust
use std::pin::pin;

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

async fn collect(stream: impl Stream) -> Vec {
    let mut vec = vec![];
    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:

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

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

fn foo(stream: S) -> impl Stream
where
    S: Stream,
{
    Foo { stream }
}

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

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

    fn poll_next(self: Pin, cx: &mut Context) -> Poll> {
        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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustasynchronousno-stdproc-macrorust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言