mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-25 17:56:49 +00:00
Reduce io::TimeoutFuture to futures_timer::TryFutureExt (#113)
This commit is contained in:
parent
d501bf6849
commit
8dff8951a6
1 changed files with 2 additions and 39 deletions
|
@ -1,12 +1,9 @@
|
||||||
use std::pin::Pin;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use futures_timer::Delay;
|
use futures_timer::TryFutureExt;
|
||||||
use pin_utils::unsafe_pinned;
|
|
||||||
|
|
||||||
use crate::future::Future;
|
use crate::future::Future;
|
||||||
use crate::io;
|
use crate::io;
|
||||||
use crate::task::{Context, Poll};
|
|
||||||
|
|
||||||
/// Awaits an I/O future or times out after a duration of time.
|
/// Awaits an I/O future or times out after a duration of time.
|
||||||
///
|
///
|
||||||
|
@ -33,39 +30,5 @@ pub async fn timeout<F, T>(dur: Duration, f: F) -> io::Result<T>
|
||||||
where
|
where
|
||||||
F: Future<Output = io::Result<T>>,
|
F: Future<Output = io::Result<T>>,
|
||||||
{
|
{
|
||||||
let f = TimeoutFuture {
|
f.timeout(dur).await
|
||||||
future: f,
|
|
||||||
delay: Delay::new(dur),
|
|
||||||
};
|
|
||||||
f.await
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TimeoutFuture<F> {
|
|
||||||
future: F,
|
|
||||||
delay: Delay,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F> TimeoutFuture<F> {
|
|
||||||
unsafe_pinned!(future: F);
|
|
||||||
unsafe_pinned!(delay: Delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, T> Future for TimeoutFuture<F>
|
|
||||||
where
|
|
||||||
F: Future<Output = io::Result<T>>,
|
|
||||||
{
|
|
||||||
type Output = F::Output;
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
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,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue