smol-rs / futures-lite

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

v2.4.0 #102

Open notgull opened 1 month ago

notgull commented 1 month ago
taiki-e commented 1 month ago
  • a "take_until" method
notgull commented 1 month ago

Good catch! I forgot to check the equivalent Iterator method, I'll fix.

notgull commented 1 month ago

Actually, on second check I think it's intentional. The idea is "run a stream in parallel with a future and stop the stream when the future stops." I think the idea is this:

let my_stream = /* ... */;
let (signal, shutdown) = async_channel::bounded::<()>(1);

// Send the shutdown signal when the user hits ctrl-c.
executor.spawn(async move {
    wait_for_user_to_hit_ctrl_c().await;
    signal.try_send(()).ok();
}).detach();

// Run until the stream stops.
while let Some(item) = stream::take_until(my_stream, shutdown).next().await {
    do_something(item).await;
}
taiki-e commented 1 month ago

(Honestly, I have no strong opinion on which API to choose here. I just know what the result of inconsistent APIs can be: https://github.com/rust-lang/futures-rs/issues/2755)

notgull commented 1 month ago

I think it's not an inconsistent API, since it's somewhat Stream-specific. However I would be okay with changing the name to distinguish it from any Iterator functions.

notgull commented 1 month ago

I think the name is the core point of confusion here. I've opened #104 to address this.

notgull commented 1 month ago

I've changed the name,