Skip to content

Commit dfdca25

Browse files
committed
feat(body): rename Entity to Payload
Closes #1464
1 parent 313f82d commit dfdca25

File tree

18 files changed

+105
-160
lines changed

18 files changed

+105
-160
lines changed

benches/end_to_end.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn get_one_at_a_time(b: &mut test::Bencher) {
3030
b.iter(move || {
3131
client.get(url.clone())
3232
.and_then(|res| {
33-
res.into_body().into_stream().for_each(|_chunk| {
33+
res.into_body().for_each(|_chunk| {
3434
Ok(())
3535
})
3636
})
@@ -55,7 +55,7 @@ fn post_one_at_a_time(b: &mut test::Bencher) {
5555
*req.method_mut() = Method::POST;
5656
*req.uri_mut() = url.clone();
5757
client.request(req).and_then(|res| {
58-
res.into_body().into_stream().for_each(|_chunk| {
58+
res.into_body().for_each(|_chunk| {
5959
Ok(())
6060
})
6161
}).wait().expect("client wait");
@@ -75,7 +75,6 @@ fn spawn_hello(rt: &mut Runtime) -> SocketAddr {
7575

7676
let service = const_service(service_fn(|req: Request<Body>| {
7777
req.into_body()
78-
.into_stream()
7978
.concat2()
8079
.map(|_| {
8180
Response::new(Body::from(PHRASE))

examples/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() {
4040
println!("Response: {}", res.status());
4141
println!("Headers: {:#?}", res.headers());
4242

43-
res.into_body().into_stream().for_each(|chunk| {
43+
res.into_body().for_each(|chunk| {
4444
io::stdout().write_all(&chunk)
4545
.map_err(|e| panic!("example expects stdout is open, error={}", e))
4646
})

examples/params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Service for ParamExample {
3232
Box::new(futures::future::ok(Response::new(INDEX.into())))
3333
},
3434
(&Method::POST, "/post") => {
35-
Box::new(req.into_parts().1.into_stream().concat2().map(|b| {
35+
Box::new(req.into_body().concat2().map(|b| {
3636
// Parse the request body. form_urlencoded::parse
3737
// always succeeds, but in general parsing may
3838
// fail (for example, an invalid post of json), so

examples/web_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Service for ResponseExamples {
4444
let web_res_future = client.request(req);
4545

4646
Box::new(web_res_future.map(|web_res| {
47-
let body = Body::wrap_stream(web_res.into_body().into_stream().map(|b| {
47+
let body = Body::wrap_stream(web_res.into_body().map(|b| {
4848
Chunk::from(format!("before: '{:?}'<br>after: '{:?}'",
4949
std::str::from_utf8(LOWERCASE).unwrap(),
5050
std::str::from_utf8(&b).unwrap()))
@@ -54,7 +54,7 @@ impl Service for ResponseExamples {
5454
},
5555
(&Method::POST, "/web_api") => {
5656
// A web api to run against. Simple upcasing of the body.
57-
let body = Body::wrap_stream(req.into_body().into_stream().map(|chunk| {
57+
let body = Body::wrap_stream(req.into_body().map(|chunk| {
5858
let upper = chunk.iter().map(|byte| byte.to_ascii_uppercase())
5959
.collect::<Vec<u8>>();
6060
Chunk::from(upper)

src/proto/body.rs renamed to src/body.rs

Lines changed: 16 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::Chunk;
1313
type BodySender = mpsc::Sender<Result<Chunk, ::Error>>;
1414

1515
/// This trait represents a streaming body of a `Request` or `Response`.
16-
pub trait Entity {
16+
pub trait Payload {
1717
/// A buffer of bytes representing a single chunk of a body.
1818
type Data: AsRef<[u8]>;
1919

@@ -63,7 +63,7 @@ pub trait Entity {
6363
}
6464
}
6565

66-
impl<E: Entity> Entity for Box<E> {
66+
impl<E: Payload> Payload for Box<E> {
6767
type Data = E::Data;
6868
type Error = E::Error;
6969

@@ -84,43 +84,10 @@ impl<E: Entity> Entity for Box<E> {
8484
}
8585
}
8686

87-
/// A wrapper to consume an `Entity` as a futures `Stream`.
88-
#[must_use = "streams do nothing unless polled"]
89-
#[derive(Debug)]
90-
pub struct EntityStream<E> {
91-
is_data_eof: bool,
92-
entity: E,
93-
}
94-
95-
impl<E: Entity> Stream for EntityStream<E> {
96-
type Item = E::Data;
97-
type Error = E::Error;
98-
99-
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
100-
loop {
101-
if self.is_data_eof {
102-
return self.entity.poll_trailers()
103-
.map(|async| {
104-
async.map(|_opt| {
105-
// drop the trailers and return that Stream is done
106-
None
107-
})
108-
});
109-
}
110-
111-
let opt = try_ready!(self.entity.poll_data());
112-
if let Some(data) = opt {
113-
return Ok(Async::Ready(Some(data)));
114-
} else {
115-
self.is_data_eof = true;
116-
}
117-
}
118-
}
119-
}
12087

121-
/// An `Entity` of `Chunk`s, used when receiving bodies.
88+
/// A `Payload` of `Chunk`s, used when receiving bodies.
12289
///
123-
/// Also a good default `Entity` to use in many applications.
90+
/// Also a good default `Payload` to use in many applications.
12491
#[must_use = "streams do nothing unless polled"]
12592
pub struct Body {
12693
kind: Kind,
@@ -229,35 +196,6 @@ impl Body {
229196
Body::new(Kind::Wrapped(Box::new(mapped)))
230197
}
231198

232-
/// Convert this `Body` into a `Stream<Item=Chunk, Error=hyper::Error>`.
233-
///
234-
/// # Example
235-
///
236-
/// ```
237-
/// # extern crate futures;
238-
/// # extern crate hyper;
239-
/// # use futures::{Future, Stream};
240-
/// # use hyper::{Body, Request};
241-
/// # fn request_concat(some_req: Request<Body>) {
242-
/// let req: Request<Body> = some_req;
243-
/// let body = req.into_body();
244-
///
245-
/// let stream = body.into_stream();
246-
/// stream.concat2()
247-
/// .map(|buf| {
248-
/// println!("body length: {}", buf.len());
249-
/// });
250-
/// # }
251-
/// # fn main() {}
252-
/// ```
253-
#[inline]
254-
pub fn into_stream(self) -> EntityStream<Body> {
255-
EntityStream {
256-
is_data_eof: false,
257-
entity: self,
258-
}
259-
}
260-
261199
/// Returns if this body was constructed via `Body::empty()`.
262200
///
263201
/// # Note
@@ -345,7 +283,7 @@ impl Default for Body {
345283
}
346284
}
347285

348-
impl Entity for Body {
286+
impl Payload for Body {
349287
type Data = Chunk;
350288
type Error = ::Error;
351289

@@ -373,6 +311,15 @@ impl Entity for Body {
373311
}
374312
}
375313

314+
impl Stream for Body {
315+
type Item = Chunk;
316+
type Error = ::Error;
317+
318+
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
319+
self.poll_data()
320+
}
321+
}
322+
376323
impl fmt::Debug for Body {
377324
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
378325
f.debug_struct("Body")
@@ -489,10 +436,10 @@ fn test_body_stream_concat() {
489436

490437
let body = Body::from("hello world");
491438

492-
let total = body.into_stream()
439+
let total = body
493440
.concat2()
494441
.wait()
495442
.unwrap();
496443
assert_eq!(total.as_ref(), b"hello world");
497-
498444
}
445+
File renamed without changes.

src/client/conn.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use futures::{Async, Future, Poll};
1515
use futures::future::{self, Either};
1616
use tokio_io::{AsyncRead, AsyncWrite};
1717

18+
use body::Payload;
1819
use proto;
19-
use proto::body::Entity;
2020
use super::dispatch;
2121
use {Body, Request, Response, StatusCode};
2222

@@ -45,7 +45,7 @@ pub struct SendRequest<B> {
4545
pub struct Connection<T, B>
4646
where
4747
T: AsyncRead + AsyncWrite,
48-
B: Entity + 'static,
48+
B: Payload + 'static,
4949
{
5050
inner: proto::dispatch::Dispatcher<
5151
proto::dispatch::Client<B>,
@@ -138,7 +138,7 @@ impl<B> SendRequest<B>
138138

139139
impl<B> SendRequest<B>
140140
where
141-
B: Entity + 'static,
141+
B: Payload + 'static,
142142
{
143143
/// Sends a `Request` on the associated connection.
144144
///
@@ -262,7 +262,7 @@ impl<B> fmt::Debug for SendRequest<B> {
262262
impl<T, B> Connection<T, B>
263263
where
264264
T: AsyncRead + AsyncWrite,
265-
B: Entity + 'static,
265+
B: Payload + 'static,
266266
{
267267
/// Return the inner IO object, and additional information.
268268
pub fn into_parts(self) -> Parts<T> {
@@ -289,7 +289,7 @@ where
289289
impl<T, B> Future for Connection<T, B>
290290
where
291291
T: AsyncRead + AsyncWrite,
292-
B: Entity + 'static,
292+
B: Payload + 'static,
293293
{
294294
type Item = ();
295295
type Error = ::Error;
@@ -302,7 +302,7 @@ where
302302
impl<T, B> fmt::Debug for Connection<T, B>
303303
where
304304
T: AsyncRead + AsyncWrite + fmt::Debug,
305-
B: Entity + 'static,
305+
B: Payload + 'static,
306306
{
307307
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
308308
f.debug_struct("Connection")
@@ -331,7 +331,7 @@ impl Builder {
331331
pub fn handshake<T, B>(&self, io: T) -> Handshake<T, B>
332332
where
333333
T: AsyncRead + AsyncWrite,
334-
B: Entity + 'static,
334+
B: Payload + 'static,
335335
{
336336
Handshake {
337337
inner: HandshakeInner {
@@ -345,7 +345,7 @@ impl Builder {
345345
pub(super) fn handshake_no_upgrades<T, B>(&self, io: T) -> HandshakeNoUpgrades<T, B>
346346
where
347347
T: AsyncRead + AsyncWrite,
348-
B: Entity + 'static,
348+
B: Payload + 'static,
349349
{
350350
HandshakeNoUpgrades {
351351
inner: HandshakeInner {
@@ -362,7 +362,7 @@ impl Builder {
362362
impl<T, B> Future for Handshake<T, B>
363363
where
364364
T: AsyncRead + AsyncWrite,
365-
B: Entity + 'static,
365+
B: Payload + 'static,
366366
{
367367
type Item = (SendRequest<B>, Connection<T, B>);
368368
type Error = ::Error;
@@ -387,7 +387,7 @@ impl<T, B> fmt::Debug for Handshake<T, B> {
387387
impl<T, B> Future for HandshakeNoUpgrades<T, B>
388388
where
389389
T: AsyncRead + AsyncWrite,
390-
B: Entity + 'static,
390+
B: Payload + 'static,
391391
{
392392
type Item = (SendRequest<B>, proto::dispatch::Dispatcher<
393393
proto::dispatch::Client<B>,
@@ -406,7 +406,7 @@ where
406406
impl<T, B, R> Future for HandshakeInner<T, B, R>
407407
where
408408
T: AsyncRead + AsyncWrite,
409-
B: Entity + 'static,
409+
B: Payload + 'static,
410410
R: proto::Http1Transaction<
411411
Incoming=StatusCode,
412412
Outgoing=proto::RequestLine,
@@ -470,15 +470,15 @@ impl<B: Send> AssertSendSync for SendRequest<B> {}
470470
impl<T: Send, B: Send> AssertSend for Connection<T, B>
471471
where
472472
T: AsyncRead + AsyncWrite,
473-
B: Entity + 'static,
473+
B: Payload + 'static,
474474
B::Data: Send + 'static,
475475
{}
476476

477477
#[doc(hidden)]
478478
impl<T: Send + Sync, B: Send + Sync> AssertSendSync for Connection<T, B>
479479
where
480480
T: AsyncRead + AsyncWrite,
481-
B: Entity + 'static,
481+
B: Payload + 'static,
482482
B::Data: Send + Sync + 'static,
483483
{}
484484

0 commit comments

Comments
 (0)