smol-rs / futures-lite

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

Add 'stream::or' and 'StreamExt::or' #25

Closed Byron closed 3 years ago

Byron commented 3 years ago

Since I basically copied and adapted the corresponding functionality from future, I am not entirely sure if the terminology and examples match the style of the stream module. In any case, it seems to work as expected, and reads nicely in code. For example, I was able to turn this…

        let mut events = futures_util::stream::select_all(vec![
            ticker(duration_per_frame).map(|_| Event::Tick).boxed(),
            key_receive.map(Event::Input).boxed(),
            events.boxed(),
        ]);

…into this…

        let mut events = ticker(duration_per_frame)
            .map(|_| Event::Tick)
            .or(key_receive.map(Event::Input))
            .or(events);

Fixes #2

PS: Feel free to squash, I tend to commit every tiny step out of fear I loose a 'good' state :D