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

implement Body for http::{Request, Response} #26

Merged
merged 1 commit into from
Dec 13, 2019
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
implement Body for http::{Request, Response}
  • Loading branch information
seanmonstar committed Dec 12, 2019
commit 25266f73e21e136901ee60cfccde3842bfe298c3
66 changes: 66 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,72 @@ impl<T: Body + Unpin + ?Sized> Body for Box<T> {
}
}

impl<B: Body> Body for http::Request<B> {
type Data = B::Data;
type Error = B::Error;

fn poll_data(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
unsafe {
self.map_unchecked_mut(http::Request::body_mut)
.poll_data(cx)
}
}

fn poll_trailers(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
unsafe {
self.map_unchecked_mut(http::Request::body_mut)
.poll_trailers(cx)
}
}

fn is_end_stream(&self) -> bool {
self.body().is_end_stream()
}

fn size_hint(&self) -> SizeHint {
self.body().size_hint()
}
}

impl<B: Body> Body for http::Response<B> {
type Data = B::Data;
type Error = B::Error;

fn poll_data(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
unsafe {
self.map_unchecked_mut(http::Response::body_mut)
.poll_data(cx)
}
}

fn poll_trailers(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
unsafe {
self.map_unchecked_mut(http::Response::body_mut)
.poll_trailers(cx)
}
}

fn is_end_stream(&self) -> bool {
self.body().is_end_stream()
}

fn size_hint(&self) -> SizeHint {
self.body().size_hint()
}
}

#[cfg(test)]
fn _assert_bounds() {
fn can_be_trait_object(_: &dyn Body<Data = std::io::Cursor<Vec<u8>>, Error = std::io::Error>) {}
Expand Down