See crate docs for details
You can use this crate to create a deadline received through a StopToken
:
```rust use asyncstd::prelude::*; use asyncstd::{stream, task};
use stoptoken::prelude::*; use stoptoken::StopSource;
use std::time::Duration;
async fn main() { // Create a stop source and generate a token. let src = StopSource::new(); let deadline = src.token();
// When stop source is dropped, the loop will stop.
// Move the source to a task, and drop it after 100 millis.
task::spawn(async move {
task::sleep(Duration::from_millis(100)).await;
drop(src);
});
// Create a stream that generates numbers until
// it receives a signal it needs to stop.
let mut work = stream::repeat(12u8).timeout_at(deadline);
// Loop over each item in the stream.
while let Some(Ok(ev)) = work.next().await {
println!("{}", ev);
}
} ```
Or Instant
to create a time
-based deadline:
```rust use asyncstd::prelude::*; use asyncstd::stream;
use stop_token::prelude::*;
use std::time::{Instant, Duration};
async fn main() { // Create a stream that generates numbers for 100 millis. let deadline = Instant::now() + Duration::frommillis(100); let mut work = stream::repeat(12u8).timeoutat(deadline);
// Loop over each item in the stream.
while let Some(Ok(ev)) = work.next().await {
println!("{}", ev);
}
} ```