Last active
January 11, 2024 02:59
-
-
Save ethe/d12fec2e86efde95a2e1a146940c9c10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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