Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

for_await macro #25

Closed
wants to merge 7 commits into from
Closed

for_await macro #25

wants to merge 7 commits into from

Conversation

yoshuawuyts
Copy link
Collaborator

@yoshuawuyts yoshuawuyts commented May 8, 2019

Description

Adds a for_await macro that can be used to parallelize spawning of items in a stream. Depends on rust-lang/futures-rs#1548 to land first.

Motivation and Context

This introduces a superset of the futures::for_await macro, where a concurrency mode can now be set in the attribute, which in turn can spawn individual tasks back on the executor.

This makes iterating over streams much more legible:

#[runtime::main]
async fn main() -> std::io::Result<()> {
    let mut listener = TcpListener::bind("127.0.0.1:8080")?;
    println!("Listening on {}", listener.local_addr()?);

    #[for_await(try_parallel)]
    for stream in listener.incoming() {
        println!("Accepting from: {}", stream.peer_addr()?);

        let (reader, writer) = &mut stream.split();
        await!(reader.copy_into(writer))?;
        Ok::<(), std::io::Error>(())
    }
    Ok(())
}

as compared to:

#[runtime::main]
async fn main() -> std::io::Result<()> {
    let mut listener = TcpListener::bind("127.0.0.1:8080")?;
    println!("Listening on {}", listener.local_addr()?);

    // accept connections and process them in parallel
    await!(listener
        .incoming()
        .try_for_each_concurrent(!0, async move |stream| {
            await!(runtime::spawn(async move {
                println!("Accepting from: {}", stream.peer_addr()?);

                let (reader, writer) = &mut stream.split();
                await!(reader.copy_into(writer))?;
                Ok::<(), std::io::Error>(())
            }))
        }))?;
    Ok(())
}

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Signed-off-by: Yoshua Wuyts <[email protected]>
Signed-off-by: Yoshua Wuyts <[email protected]>
Signed-off-by: Yoshua Wuyts <[email protected]>
Signed-off-by: Yoshua Wuyts <[email protected]>
Signed-off-by: Yoshua Wuyts <[email protected]>
Signed-off-by: Yoshua Wuyts <[email protected]>
@yoshuawuyts yoshuawuyts changed the title For each macro for_await macro May 8, 2019
Signed-off-by: Yoshua Wuyts <[email protected]>
@taiki-e
Copy link
Contributor

taiki-e commented Aug 10, 2019

@yoshuawuyts rust-lang/futures-rs#1548 was closed and published as futures-async-stream. Thanks for your patience!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants