toy_async_runtime/
features.rs

1//! Module to provide some kind of feature to the user.
2
3use std::future::Future;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6use std::time::SystemTime;
7
8/// Future sleep function
9pub struct Sleep {
10    /// When the future is created
11    now: SystemTime,
12    /// Waiting time specified by the user.
13    waiting_ms: u128,
14}
15
16/// Plain Sleep impl.
17impl Sleep {
18    pub fn new(ms: u128) -> Self {
19        Sleep {
20            now: SystemTime::now(),
21            waiting_ms: ms,
22        }
23    }
24}
25
26impl Future for Sleep {
27    type Output = ();
28
29    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
30        if self.now.elapsed().unwrap().as_millis() >= self.waiting_ms {
31            Poll::Ready(())
32        } else {
33            Poll::Pending
34        }
35    }
36}
37
38// TODO: missing unit testing!