futures-rs icon indicating copy to clipboard operation
futures-rs copied to clipboard

FuturesUnordered + HashMap?

Open allsey87 opened this issue 4 years ago • 10 comments

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.

allsey87 avatar Jan 26 '21 12:01 allsey87

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.

taiki-e avatar Feb 13 '21 06:02 taiki-e

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.

bootrecords avatar Apr 26 '21 16:04 bootrecords

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

ibraheemdev avatar May 06 '21 05:05 ibraheemdev

The latter would probably be much easier.

Yeah. I would prefer the latter.

taiki-e avatar May 06 '21 05:05 taiki-e

Really looking forward to use it

mitinarseny avatar Nov 24 '22 11:11 mitinarseny

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 avatar Jun 10 '23 03:06 StoicDeveloper

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

StoicDeveloper avatar Jun 10 '23 19:06 StoicDeveloper

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

allsey87 avatar Jun 11 '23 11:06 allsey87

@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.

StoicDeveloper avatar Jun 11 '23 13:06 StoicDeveloper

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.

thomaseizinger avatar Nov 14 '23 22:11 thomaseizinger