Skip to content

async_for! #2759

Open
Open
@khoover

Description

@khoover

Searching through past issues didn't turn anything up, but it seems like a killer feature for streams would be, roughly, async for x in stream { ... }. While this doesn't work currently, it seems relatively straightforward to write a macro_rule! for it?

pub fn assert_stream<S: Stream>(s: S) -> S { s }

macro_rules! async_for {
    ($var:ident in $stream:expr, $blk:block) => {
        {
            let mut stream = ::core::pin::pin!(assert_stream($stream));
            while let Some($var) = ::futures::stream::StreamExt::next(&mut stream).await {
                $blk
            }
        }
    }
}

A simple test like

async fn test_async_for<S: Stream<Item = u8>>(mut stream: S) {
    let mut stream = core::pin::pin!(stream); // Commenting this line out makes building fail.
    async_for!(x in stream.by_ref(), {
        println!("{}", x);
        break;
    });
    println!("{:?}", stream.next().await);
}

worked just fine using the macro.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-streamArea: futures::stream

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions