Skip to content
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

Add Body::poll_progress #90

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions http-body-util/src/combinators/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl<'a, T: Body + Unpin + ?Sized> Future for Frame<'a, T> {
type Output = Option<Result<http_body::Frame<T::Data>, T::Error>>;

fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
let _ = Pin::new(&mut self.0).poll_progress(ctx)?;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example of the slightly silly way this ends up being used to preserve a Poll<Result<_, _>> return type.

Pin::new(&mut self.0).poll_frame(ctx)
}
}
9 changes: 9 additions & 0 deletions http-body-util/src/combinators/map_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ where
}
}

fn poll_progress(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let this = self.project();
match this.inner.poll_progress(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
Poll::Ready(Err(err)) => Poll::Ready(Err((this.f)(err))),
}
}

fn is_end_stream(&self) -> bool {
self.inner.is_end_stream()
}
Expand Down
4 changes: 4 additions & 0 deletions http-body-util/src/combinators/map_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ where
}
}

fn poll_progress(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().inner.poll_progress(cx)
}

fn is_end_stream(&self) -> bool {
self.inner.is_end_stream()
}
Expand Down
7 changes: 7 additions & 0 deletions http-body-util/src/combinators/with_trailers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ where
}
}

fn poll_progress(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project().state.project() {
StateProj::PollBody { body, .. } => body.poll_progress(cx),
_ => Poll::Ready(Ok(())),
}
}

#[inline]
fn size_hint(&self) -> http_body::SizeHint {
match &self.state {
Expand Down
9 changes: 9 additions & 0 deletions http-body-util/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ where
}
}

fn poll_progress(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project() {
EitherProj::Left(left) => left.poll_progress(cx).map(|poll| poll.map_err(Into::into)),
EitherProj::Right(right) => {
right.poll_progress(cx).map(|poll| poll.map_err(Into::into))
}
}
}

fn is_end_stream(&self) -> bool {
match self {
Either::Left(left) => left.is_end_stream(),
Expand Down
7 changes: 7 additions & 0 deletions http-body-util/src/limited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ where
Poll::Ready(res)
}

fn poll_progress(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project()
.inner
.poll_progress(cx)
.map(|poll| poll.map_err(Into::into))
}

fn is_end_stream(&self) -> bool {
self.inner.is_end_stream()
}
Expand Down
20 changes: 20 additions & 0 deletions http-body/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ pub trait Body {
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>>;

/// Attempt to progress the body's state without pulling a new frame.
///
/// `Body` consumers can use this method to allow the `Body` implementation to continue to
/// perform work even when the consumer is not yet ready to read the next frame. For example,
/// a `Body` implementation could maintain a timer counting down between `poll_frame` calls and
/// report an error from `poll_progress` when time expires.
///
/// Consumers are *not* required to call this method. A `Body` implementation should not depend
/// on calls to `poll_progress` to occur.
///
/// An error returned from this method is considered to be equivalent to an error returned from
/// `poll_frame`.
///
/// Implementations must allow additional calls to this method after it returns
/// `Poll::Ready(Ok(())`.
fn poll_progress(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let _ = cx;
Poll::Ready(Ok(()))
}

/// Returns `true` when the end of stream has been reached.
///
/// An end of stream means that `poll_frame` will return `None`.
Expand Down