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 37 38 39 40 41 42 43 44 45
use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; use std::time::{Duration, Instant}; use crate::io::Timer; pub struct Delay { timer: Timer, } pub fn delay_until(deadline: Instant) -> Delay { Delay { timer: Timer::new(deadline), } } pub fn delay_for(duration: Duration) -> Delay { delay_until(Instant::now() + duration) } impl Delay { pub fn deadline(&self) -> Instant { self.timer.deadline() } pub fn is_elapsed(&self) -> bool { self.timer.is_elapsed() } pub fn reset(&mut self, deadline: Instant) { self.timer.reset(deadline); } } impl Future for Delay { type Output = (); fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match Pin::new(&mut self.timer).poll(cx) { Poll::Ready(_) => Poll::Ready(()), Poll::Pending => Poll::Pending, } } }