Skip to content

Instantly share code, notes, and snippets.

@ethe
Last active January 9, 2024 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethe/5360b60d1bacd2cd8fe1755c2f699ca3 to your computer and use it in GitHub Desktop.
Save ethe/5360b60d1bacd2cd8fe1755c2f699ca3 to your computer and use it in GitHub Desktop.
use std::pin::pin;
use std::time::{Duration, Instant};
use async_io::{block_on, Timer};
use async_stream::stream;
use futures::StreamExt;
use futures_lite::future::yield_now;
async fn item() -> Duration {
let start = Instant::now();
yield_now().await;
Instant::now().duration_since(start)
}
fn main() {
let factory = stream! {
loop {
yield item();
}
}
.buffered(5);
let mut stream = pin!(factory);
let result = block_on(async {
let mut result = vec![];
for _ in 0..5 {
result.push(stream.next().await.unwrap());
Timer::after(Duration::from_millis(500)).await;
}
result
});
dbg!(result);
// [examples/buffered_stream.rs:35:5] result = [
// 612.875µs,
// 501.832917ms,
// 1.002531209s,
// 1.503673417s,
// 2.004864417s, <---- what the fuck?
// ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment