2020-12-26 17:05:51 +01:00
|
|
|
use crate::stream_ext::Fuse;
|
|
|
|
|
use crate::Stream;
|
2020-12-15 23:24:38 -05:00
|
|
|
use tokio::time::{Instant, Sleep};
|
2020-01-24 17:22:56 -06:00
|
|
|
|
|
|
|
|
use core::future::Future;
|
|
|
|
|
use core::pin::Pin;
|
|
|
|
|
use core::task::{Context, Poll};
|
|
|
|
|
use pin_project_lite::pin_project;
|
2020-12-15 23:24:38 -05:00
|
|
|
use std::fmt;
|
2020-01-24 17:22:56 -06:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
pin_project! {
|
|
|
|
|
/// Stream returned by the [`timeout`](super::StreamExt::timeout) method.
|
|
|
|
|
#[must_use = "streams do nothing unless polled"]
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Timeout<S> {
|
|
|
|
|
#[pin]
|
|
|
|
|
stream: Fuse<S>,
|
2020-12-16 21:51:34 -08:00
|
|
|
#[pin]
|
2020-10-08 22:35:12 -05:00
|
|
|
deadline: Sleep,
|
2020-01-24 17:22:56 -06:00
|
|
|
duration: Duration,
|
|
|
|
|
poll_deadline: bool,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 23:24:38 -05:00
|
|
|
/// Error returned by `Timeout`.
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
2020-12-24 01:27:58 +09:00
|
|
|
pub struct Elapsed(());
|
2020-12-15 23:24:38 -05:00
|
|
|
|
2020-01-24 17:22:56 -06:00
|
|
|
impl<S: Stream> Timeout<S> {
|
|
|
|
|
pub(super) fn new(stream: S, duration: Duration) -> Self {
|
|
|
|
|
let next = Instant::now() + duration;
|
2020-12-15 23:24:38 -05:00
|
|
|
let deadline = tokio::time::sleep_until(next);
|
2020-01-24 17:22:56 -06:00
|
|
|
|
|
|
|
|
Timeout {
|
|
|
|
|
stream: Fuse::new(stream),
|
|
|
|
|
deadline,
|
|
|
|
|
duration,
|
|
|
|
|
poll_deadline: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S: Stream> Stream for Timeout<S> {
|
|
|
|
|
type Item = Result<S::Item, Elapsed>;
|
|
|
|
|
|
2020-12-16 21:51:34 -08:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
|
let me = self.project();
|
|
|
|
|
|
|
|
|
|
match me.stream.poll_next(cx) {
|
2020-01-24 17:22:56 -06:00
|
|
|
Poll::Ready(v) => {
|
|
|
|
|
if v.is_some() {
|
2020-12-16 21:51:34 -08:00
|
|
|
let next = Instant::now() + *me.duration;
|
|
|
|
|
me.deadline.reset(next);
|
|
|
|
|
*me.poll_deadline = true;
|
2020-01-24 17:22:56 -06:00
|
|
|
}
|
|
|
|
|
return Poll::Ready(v.map(Ok));
|
|
|
|
|
}
|
|
|
|
|
Poll::Pending => {}
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-16 21:51:34 -08:00
|
|
|
if *me.poll_deadline {
|
|
|
|
|
ready!(me.deadline.poll(cx));
|
|
|
|
|
*me.poll_deadline = false;
|
2020-12-24 01:27:58 +09:00
|
|
|
return Poll::Ready(Some(Err(Elapsed::new())));
|
2020-01-24 17:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Poll::Pending
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
self.stream.size_hint()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-15 23:24:38 -05:00
|
|
|
|
|
|
|
|
// ===== impl Elapsed =====
|
|
|
|
|
|
2020-12-24 01:27:58 +09:00
|
|
|
impl Elapsed {
|
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
|
Elapsed(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 23:24:38 -05:00
|
|
|
impl fmt::Display for Elapsed {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
"deadline has elapsed".fmt(fmt)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::error::Error for Elapsed {}
|
|
|
|
|
|
|
|
|
|
impl From<Elapsed> for std::io::Error {
|
|
|
|
|
fn from(_err: Elapsed) -> std::io::Error {
|
|
|
|
|
std::io::ErrorKind::TimedOut.into()
|
|
|
|
|
}
|
|
|
|
|
}
|