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

FuturesUnordered + HashMap? #2329

Open
allsey87 opened this issue Jan 26, 2021 · 10 comments
Open

FuturesUnordered + HashMap? #2329

allsey87 opened this issue Jan 26, 2021 · 10 comments
Labels
A-stream Area: futures::stream C-feature-request

Comments

@allsey87
Copy link

I would like to have something like FuturesUnordered but where the collection behaves more like a HashMap (so that I can easily look up a future by its key instead of using .iter().find() etc). Has this been considered and if so, is there any reason this doesn't exist?

I guess there is a good reason why the following also doesn't exist, but it would be awesome if somehow FuturesOrdered and FutureUnordered could have been traits that were automatically implemented for any collection where the stored type implements Future.

@taiki-e taiki-e added A-stream Area: futures::stream C-feature-request labels Jan 30, 2021
@taiki-e
Copy link
Member

taiki-e commented Feb 13, 2021

I think it's a good idea to add something like a map.
In particular, there is currently no way to remove a specific future from the FuturesUnordered.

@bootrecords
Copy link

I'd think one could obtain that kind of behaviour with tokio's StreamMap, but of course might not hurt to have a structure more geared towards Futures as compared to Streams.

@ibraheemdev
Copy link
Member

Would it be better to modify FuturesUnordered or add a new FutureMap type? The latter would probably be much easier.

@taiki-e
Copy link
Member

taiki-e commented May 6, 2021

The latter would probably be much easier.

Yeah. I would prefer the latter.

@mitinarseny
Copy link

Really looking forward to use it

@StoicDeveloper
Copy link

StoicDeveloper commented Jun 10, 2023

I went ahead and wrote a library based on FuturesUnordered that does exactly this: https://github.com/StoicDeveloper/mapped_futures. I aim to publish it as a crate once I have some feedback.

@StoicDeveloper
Copy link

Here is the crate that covers this issue: https://crates.io/crates/mapped_futures

@allsey87
Copy link
Author

@StoicDeveloper what was the motivation for making a separate crate instead of opening a PR, here?

@StoicDeveloper
Copy link

StoicDeveloper commented Jun 11, 2023

@StoicDeveloper what was the motivation for making a separate crate instead of opening a PR, here?

@allsey87 Honestly, it had not occurred to me to do so. I haven't really contributed to open source projects before. Mostly, I wanted to start using the feature immediately, whereas opening a PR would involve more discussion and require approval by the owners of this repo by meeting their standards, though of course that would be a very good thing for verifying the soundness of my unsafe changes as well as ensuring the code's quality. I think my task next will be to do just that.

@thomaseizinger
Copy link
Contributor

We had a similar need with the added requirements of bounding the Futures and Streams in their execution time and also capping the size of how many are allowed to execute at any one time.

In case it is of interest, the code is here: https://github.com/libp2p/rust-libp2p/tree/master/misc/futures-bounded/src.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-stream Area: futures::stream C-feature-request
Projects
None yet
Development

No branches or pull requests

7 participants