smol-rs / futures-lite

Futures, streams, and async I/O combinators.
Apache License 2.0
449 stars 26 forks source link

`or` but for different output types #98

Closed Einliterflasche closed 6 months ago

Einliterflasche commented 6 months ago

Hey,

first of all, great work! I don't think the smol project get's enough recognition for its amazing work.

I learned from #65 that you have an or function which is like select! except in that it requires both futures to have the same output type. Is there already a function like or which works for two futures with distinct output types and returns an Either enum?

If there is no such function as of yet I would like to try my hands at implementing it. I have thought of two issues:

  1. what to name the function
  2. this necessitates an Either struct we would either (ba dum tsss) need to implement ourselves or depend on an an (albeit small) either crate.

I think this would be a very useful and straight forward addition.

notgull commented 6 months ago

I think you can just use the either crate in combination with or to do this. For example:

let a = async { /* ... */ };
let b = async { /* ... */ };

let combo = async move { Either::Left(a.await) }.or(async move { Either::Right(b.await) });
match combo.await {
    Either::Left(a) => /* ... */,
    Either::Right(b) => /* ... */
}
Einliterflasche commented 6 months ago

True! thanks for the quick reply