futures-rs
futures-rs copied to clipboard
Add `BufferedLatest` stream combinator
We already have buffered and buffer_unordered.
Both of them keep an internal buffer of futures and run each future to completion.
My proposal is to add buffered_latest and buffer_latest_unordered methods which only keep the latest n futures in the buffer, discarding the previous ones.
Calling buffered_latest(1) means keeping only one future (the latest available one) in flight.
In the following example, there's only one request in flight at any time and clicking the button discards the previous future and restarts the download:
let stream_of_clicks = /*get stream of clicks from GUI*/;
stream_of_clicks
.map(|_| download_data())
.buffered_latest(1)
.for_each(|data| println!("Finished downloading {}", data));