diff --git a/src/io/timeout.rs b/src/io/timeout.rs index 36112cb6..6d777376 100644 --- a/src/io/timeout.rs +++ b/src/io/timeout.rs @@ -1,12 +1,9 @@ -use std::pin::Pin; use std::time::Duration; -use futures_timer::Delay; -use pin_utils::unsafe_pinned; +use futures_timer::TryFutureExt; use crate::future::Future; use crate::io; -use crate::task::{Context, Poll}; /// Awaits an I/O future or times out after a duration of time. /// @@ -33,39 +30,5 @@ pub async fn timeout(dur: Duration, f: F) -> io::Result where F: Future>, { - let f = TimeoutFuture { - future: f, - delay: Delay::new(dur), - }; - f.await -} - -struct TimeoutFuture { - future: F, - delay: Delay, -} - -impl TimeoutFuture { - unsafe_pinned!(future: F); - unsafe_pinned!(delay: Delay); -} - -impl Future for TimeoutFuture -where - F: Future>, -{ - type Output = F::Output; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.as_mut().future().poll(cx) { - Poll::Ready(v) => Poll::Ready(v), - Poll::Pending => match self.delay().poll(cx) { - Poll::Ready(_) => Poll::Ready(Err(io::Error::new( - io::ErrorKind::TimedOut, - "I/O operation has timed out", - ))), - Poll::Pending => Poll::Pending, - }, - } - } + f.timeout(dur).await }