Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StreamExt::wait_until combinator #2542

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jerry73204
Copy link

The stream.wait_until(fut) combinator lets the stream to wait until the future resovles. The stream start taking elements if the future resolves to true, otherwise the stream fuses if it resolve to false.

It is useful for initialization purpose. For example, the stream has to wait for user configuration to be ready to start producing values, and fuse the stream if the configuration is not correct. The code below illustrates the idea.

// Suppose a oneshot channel to receive the acknowledgement from user.
let (tx, rx) = oneshot::channel::<bool>();

// The sender is passed to user.
give_to_user(tx);

stream::iter(0..100)
    .wait_for(async move {
        if let Ok(yes) = rx.await {
            // forward to user opinion
            yes
        } else {
            // the sender is dropped so fuse the stream
            false
        }
    })
    .for_each(|val| async move { /* do something */ })
    .await;

A practical example is in my par-stream crate. To broadcast the messages from a stream, it creates a builder to user. The user registers receivers using the builder. The receivers should wait until the builder is finished or dropped. It ensures that every receiver can read the first value from stream.

The `stream.wait_until(fut)` combinator lets the stream to wait until
the future resovles. The stream start taking elements if the future
resolves to true, otherwise the stream fuses if it resolve to false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant