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
use futures::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::thread;
use std::time::Duration;
pub fn sleep(duration: Duration) -> Sleep {
Sleep {
thread: None,
duration,
}
}
#[derive(Debug)]
pub struct Sleep {
thread: Option<thread::JoinHandle<()>>,
duration: Duration,
}
impl Future for Sleep {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.thread.is_none() {
let waker = cx.waker().clone();
let duration = self.duration;
self.get_mut().thread = Some(thread::spawn(move || {
thread::sleep(duration);
waker.wake();
}));
Poll::Pending
} else {
Poll::Ready(())
}
}
}