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