Skip to content

make StreamExt::timeout(d).next() behave like future::timeout(d, s.next()) #732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Dec 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/stream/stream/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,36 @@ pin_project! {
stream: S,
#[pin]
delay: Timer,
duration: Duration,
}
}

impl<S: Stream> Timeout<S> {
pub(crate) fn new(stream: S, dur: Duration) -> Self {
let delay = timer_after(dur);

Self { stream, delay }
Self { stream, delay, duration: dur }
}
}

impl<S: Stream> Stream for Timeout<S> {
type Item = Result<S::Item, TimeoutError>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
let mut this = self.project();

match this.stream.poll_next(cx) {
let r = match this.stream.poll_next(cx) {
Poll::Ready(Some(v)) => Poll::Ready(Some(Ok(v))),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => match this.delay.poll(cx) {
Poll::Pending => match this.delay.as_mut().poll(cx) {
Poll::Ready(_) => Poll::Ready(Some(Err(TimeoutError { _private: () }))),
Poll::Pending => Poll::Pending,
Poll::Pending => return Poll::Pending,
},
}
};

*this.delay.as_mut() = timer_after(*this.duration);

r
}
}

Expand Down