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

Provide an `or!` macro that handles any number of futures

Open joshtriplett opened this issue 5 years ago • 16 comments

This has the same effect as f1.or(f2.or(f3.or(...))) but without the nesting.

Note that an equivalent race! macro would not be fair, since it would be a coin-flip at each level. A fair race! macro would need to count the number of futures, and select randomly. This could be done by adding a version of the Race future that has a count and checks its first future first with 1/N probability, and its second future first otherwise.

joshtriplett avatar Aug 04 '20 23:08 joshtriplett

@stjepang That's doable. Could you live with it appearing as both futures_lite::or_futures! (or similar placeholder) and as futures_lite::future::or!? (The alternative would, as far as I can tell, require a separate helper crate.)

joshtriplett avatar Aug 09 '20 05:08 joshtriplett

Hmm, I would be okay with a helper crate, perhaps futures-lite-macros that would allow us to export the macro into the right module. It's not like such a crate would materially impact compilation times, and I expect in the future rust version the hack will not be necessary.

ghost avatar Aug 10 '20 12:08 ghost

Alright, I can try that. (It'd look a lot like this, but with the macro implementations moved to that helper crate, and then pub used here.)

joshtriplett avatar Aug 10 '20 20:08 joshtriplett

Sounds good!

ghost avatar Aug 11 '20 08:08 ghost

I loved this so much I stole it for futures-micro. Also I adapted it for zip/join with mildly amusing results

jjl avatar Aug 11 '20 09:08 jjl

You could actually just depend on futures-micro to get this macro now it's released, instead of extracting it out into a new crate. You'd also get an implementation of PollFn (which is itself about a third of the crate size), and i was suggesting you nick PollState anyway, which is another third of it.

jjl avatar Aug 11 '20 18:08 jjl

That’s actually a good idea :)

ghost avatar Aug 11 '20 20:08 ghost

@jjl: I think the implementation of zip should pattern-match out of the nested 2-tuples, and emit an N-tuple. That isn't hard to do in a macro, and it would substantially improve usability.

joshtriplett avatar Aug 12 '20 01:08 joshtriplett

i completely agree, I just haven't gotten around to figuring it out yet.

jjl avatar Aug 12 '20 11:08 jjl

now done

jjl avatar Aug 13 '20 10:08 jjl

I would use this :)

jacobrosenthal avatar Aug 27 '20 00:08 jacobrosenthal

you already can, it's in the released futures-micro

jjl avatar Aug 27 '20 05:08 jjl

I am using it, id be happy to see a reexport if that happens.

On Wed, Aug 26, 2020 at 10:11 PM jjl [email protected] wrote:

you already can, it's in the released futures-micro

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/stjepang/futures-lite/pull/7#issuecomment-681496290, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADPI5BFTTU7ABAIZOUEV4TSCXTG7ANCNFSM4PU5A2ZA .

jacobrosenthal avatar Aug 27 '20 05:08 jacobrosenthal

there's a PR for that! #15

jjl avatar Aug 27 '20 05:08 jjl

Is there still any interest in this?

Personally I'm a fan of the fact that futures-lite doesn't contain any obscure macros right now, and I think that a.or(b).or(c) looks better than or!(a, b, c). Still, it looks like people were interested in this above, so there might still be demand for it now.

notgull avatar Apr 28 '23 18:04 notgull

a.or(b).or(c)

I think such a chain is basically an anti-pattern because polling will become unfair. https://github.com/tokio-rs/tokio/issues/2319

taiki-e avatar Jul 16 '23 10:07 taiki-e