microasync_util/
timer.rs

1extern crate std;
2
3use core::{
4    future::Future,
5    pin::Pin,
6    task::{Context, Poll},
7};
8use std::time::{Duration, SystemTime};
9
10pub struct Timer {
11    length: Duration,
12    start: SystemTime,
13}
14
15impl Future for Timer {
16    type Output = ();
17
18    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
19        if self.start.elapsed().unwrap_or(self.length) >= self.length {
20            Poll::Ready(())
21        } else {
22            Poll::Pending
23        }
24    }
25}
26
27pub async fn wait(duration: Duration) {
28    Timer {
29        length: duration,
30        start: SystemTime::now(),
31    }
32    .await
33}
34
35pub async fn wait_ms(ms: u64) {
36    Timer {
37        length: Duration::from_millis(ms),
38        start: SystemTime::now(),
39    }
40    .await
41}