Skip to content

Commit

Permalink
implement Body for http::{Request, Response}
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Dec 12, 2019
1 parent e1a847c commit 25266f7
Showing 1 changed file with 66 additions and 0 deletions.
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

0 comments on commit 25266f7

Please sign in to comment.