Skip to content

Instantly share code, notes, and snippets.

@ethe
Last active January 11, 2024 02:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ethe/d12fec2e86efde95a2e1a146940c9c10 to your computer and use it in GitHub Desktop.
Save ethe/d12fec2e86efde95a2e1a146940c9c10 to your computer and use it in GitHub Desktop.
use std::time::{Duration, Instant};
use async_io::{block_on, Timer};
use async_stream::stream;
use futures::StreamExt;
use futures_lite::future::yield_now;
fn main() {
let result = block_on(async {
stream! {
loop {
yield async {
let start = Instant::now();
yield_now().await;
Instant::now().duration_since(start)
};
}
}
.buffered(5)
.take(5)
.then(|d| async move {
Timer::after(Duration::from_millis(500)).await;
d
})
.collect::<Vec<_>>()
.await
});
dbg!(result);
// [examples/buffered_stream.rs:26:5] result = [
// 612.875µs,
// 501.832917ms,
// 1.002531209s,
// 1.503673417s,
// 2.004864417s, <---- ???
// ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment