Skip to content

Commit 2d9f349

Browse files
LucioFrancoseanmonstar
authored andcommitted
feat(body): implement http_body::Body for hyper::Body
This adds a `http_body::Body` impl for hypers `Body`. This should allow us to start moving to a more generic body trait based on `BufStream` and `http-body`.
1 parent 973f981 commit 2d9f349

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bytes = "0.4.4"
2222
futures = "0.1.21"
2323
futures-cpupool = { version = "0.1.6", optional = true }
2424
http = "0.1.15"
25+
http-body = "0.1"
2526
httparse = "1.0"
2627
h2 = "0.1.10"
2728
iovec = "0.1"
@@ -30,6 +31,7 @@ log = "0.4"
3031
net2 = { version = "0.2.32", optional = true }
3132
time = "0.1"
3233
tokio = { version = "0.1.14", optional = true, default-features = false, features = ["rt-full"] }
34+
tokio-buf = "0.1"
3335
tokio-executor = { version = "0.1.0", optional = true }
3436
tokio-io = "0.1"
3537
tokio-reactor = { version = "0.1", optional = true }

src/body/body.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt;
44
use bytes::Bytes;
55
use futures::sync::{mpsc, oneshot};
66
use futures::{Async, Future, Poll, Stream, Sink, AsyncSink, StartSend};
7+
use tokio_buf::SizeHint;
78
use h2;
89
use http::HeaderMap;
910

@@ -332,6 +333,36 @@ impl Payload for Body {
332333
}
333334
}
334335

336+
impl ::http_body::Body for Body {
337+
type Data = Chunk;
338+
type Error = ::Error;
339+
340+
fn poll_data(&mut self) -> Poll<Option<Self::Data>, Self::Error> {
341+
<Self as Payload>::poll_data(self)
342+
}
343+
344+
fn poll_trailers(&mut self) -> Poll<Option<HeaderMap>, Self::Error> {
345+
<Self as Payload>::poll_trailers(self)
346+
}
347+
348+
fn is_end_stream(&self) -> bool {
349+
<Self as Payload>::is_end_stream(self)
350+
}
351+
352+
fn size_hint(&self) -> SizeHint {
353+
let mut hint = SizeHint::default();
354+
355+
let content_length = <Self as Payload>::content_length(self);
356+
357+
if let Some(size) = content_length {
358+
hint.set_upper(size);
359+
hint.set_lower(size)
360+
}
361+
362+
hint
363+
}
364+
}
365+
335366
impl Stream for Body {
336367
type Item = Chunk;
337368
type Error = ::Error;

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ extern crate bytes;
2222
#[cfg(feature = "runtime")] extern crate futures_cpupool;
2323
extern crate h2;
2424
#[doc(hidden)] pub extern crate http;
25+
extern crate http_body;
2526
extern crate httparse;
2627
extern crate iovec;
2728
extern crate itoa;
2829
#[macro_use] extern crate log;
2930
#[cfg(feature = "runtime")] extern crate net2;
3031
extern crate time;
3132
#[cfg(feature = "runtime")] extern crate tokio;
33+
extern crate tokio_buf;
3234
#[cfg(feature = "runtime")] extern crate tokio_executor;
3335
#[macro_use] extern crate tokio_io;
3436
#[cfg(feature = "runtime")] extern crate tokio_reactor;

0 commit comments

Comments
 (0)