Skip to content

Commit 8dff895

Browse files
kppStjepan Glavina
authored and
Stjepan Glavina
committed
Reduce io::TimeoutFuture to futures_timer::TryFutureExt (#113)
1 parent d501bf6 commit 8dff895

File tree

1 file changed

+2
-39
lines changed

1 file changed

+2
-39
lines changed

src/io/timeout.rs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::pin::Pin;
21
use std::time::Duration;
32

4-
use futures_timer::Delay;
5-
use pin_utils::unsafe_pinned;
3+
use futures_timer::TryFutureExt;
64

75
use crate::future::Future;
86
use crate::io;
9-
use crate::task::{Context, Poll};
107

118
/// Awaits an I/O future or times out after a duration of time.
129
///
@@ -33,39 +30,5 @@ pub async fn timeout<F, T>(dur: Duration, f: F) -> io::Result<T>
3330
where
3431
F: Future<Output = io::Result<T>>,
3532
{
36-
let f = TimeoutFuture {
37-
future: f,
38-
delay: Delay::new(dur),
39-
};
40-
f.await
41-
}
42-
43-
struct TimeoutFuture<F> {
44-
future: F,
45-
delay: Delay,
46-
}
47-
48-
impl<F> TimeoutFuture<F> {
49-
unsafe_pinned!(future: F);
50-
unsafe_pinned!(delay: Delay);
51-
}
52-
53-
impl<F, T> Future for TimeoutFuture<F>
54-
where
55-
F: Future<Output = io::Result<T>>,
56-
{
57-
type Output = F::Output;
58-
59-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
60-
match self.as_mut().future().poll(cx) {
61-
Poll::Ready(v) => Poll::Ready(v),
62-
Poll::Pending => match self.delay().poll(cx) {
63-
Poll::Ready(_) => Poll::Ready(Err(io::Error::new(
64-
io::ErrorKind::TimedOut,
65-
"I/O operation has timed out",
66-
))),
67-
Poll::Pending => Poll::Pending,
68-
},
69-
}
70-
}
33+
f.timeout(dur).await
7134
}

0 commit comments

Comments
 (0)