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 merge_round_robin for StreamExt #2930

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

RustyNova016
Copy link

This function allows merging two streams using a custom "round robin" policy.

This is an useful alternative to chain as the second stream doesn't wait for the first to finish, and both can be interweaved toghether

Example:

use futures::stream::{self, StreamExt};
let stream1 = stream::iter(vec!["a", "a"]);
let stream2 = stream::iter(vec!["b", "b", "b", "b", "c"]);
let stream = stream1.merge_round_robin(stream2, 1, 2);
let result: Vec<_> = stream.collect().await;
assert_eq!(result, vec![
    "a",
    "b",
    "b",
    "a",
    "b",
    "b",
    "c"
]);

I am quite new to contributing to the official channels, so if there's anything wrong, please tell!

@rustbot rustbot added A-stream Area: futures::stream S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 27, 2025
@taiki-e
Copy link
Member

taiki-e commented Mar 23, 2025

Thanks for the PR. What is the difference compared to stream::select and stream::select_with_strategy?

https://docs.rs/futures/latest/futures/stream/fn.select.html
https://docs.rs/futures/latest/futures/stream/fn.select_with_strategy.html#round-robin

@taiki-e taiki-e added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 23, 2025
@RustyNova016
Copy link
Author

Well... I admit I didn't see that stream::select existed.

But there's a couple changes:

  • It's not a standalone function, and is part of StreamExt. So it can be inlined
  • The strategy is easier to implement. If we want to pass 2 items from stream A and 1 item from stream B, we only have to set the numbers. Doing it with select_with_strategy would require either storing state in a mutex or use enumerate and modulos

But I understand if it feels too specific when we already have something else implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-stream Area: futures::stream S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants