Skip to content

Commit 4b6099c

Browse files
committed
feat(body): implement HttpBody for Request and Response
When the body type of a `Request` or `Response` implements `HttpBody`, the `Request` or `Response` itself now implements `HttpBody`. This allows writing things like `hyper::body::aggregate(req)` instead of `hyper::body::aggregate(req.into_body())`. Closes #2067
1 parent bfda390 commit 4b6099c

File tree

11 files changed

+21
-21
lines changed

11 files changed

+21
-21
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ futures-core = { version = "0.3", default-features = false }
2525
futures-channel = "0.3"
2626
futures-util = { version = "0.3", default-features = false }
2727
http = "0.2"
28-
http-body = "0.3"
28+
http-body = "0.3.1"
2929
httparse = "1.0"
3030
h2 = "0.2.1"
3131
itoa = "0.4.1"

examples/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async fn fetch_url(url: hyper::Uri) -> Result<()> {
4242

4343
// Stream the body, writing each chunk to stdout as we get it
4444
// (instead of buffering and printing at the end).
45-
while let Some(next) = res.body_mut().data().await {
45+
while let Some(next) = res.data().await {
4646
let chunk = next?;
4747
io::stdout().write_all(&chunk).await?;
4848
}

examples/client_json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
3030
let res = client.get(url).await?;
3131

3232
// asynchronously aggregate the chunks of the body
33-
let body = hyper::body::aggregate(res.into_body()).await?;
33+
let body = hyper::body::aggregate(res).await?;
3434

3535
// try to parse as json with serde_json
3636
let users = serde_json::from_reader(body.reader())?;

examples/params.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
1717
(&Method::GET, "/") | (&Method::GET, "/post") => Ok(Response::new(INDEX.into())),
1818
(&Method::POST, "/post") => {
1919
// Concatenate the body...
20-
let b = hyper::body::to_bytes(req.into_body()).await?;
20+
let b = hyper::body::to_bytes(req).await?;
2121
// Parse the request body. form_urlencoded::parse
2222
// always succeeds, but in general parsing may
2323
// fail (for example, an invalid post of json), so

examples/web_api.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async fn client_request_response(client: &Client<HttpConnector>) -> Result<Respo
4040

4141
async fn api_post_response(req: Request<Body>) -> Result<Response<Body>> {
4242
// Aggregate the body...
43-
let whole_body = hyper::body::aggregate(req.into_body()).await?;
43+
let whole_body = hyper::body::aggregate(req).await?;
4444
// Decode as JSON...
4545
let mut data: serde_json::Value = serde_json::from_reader(whole_body.reader())?;
4646
// Change the JSON...

src/body/body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type BodySender = mpsc::Sender<Result<Bytes, crate::Error>>;
1818

1919
/// A stream of `Bytes`, used when receiving bodies.
2020
///
21-
/// A good default [`HttpBody`](crates::body::HttpBody) to use in many
21+
/// A good default [`HttpBody`](crate::body::HttpBody) to use in many
2222
/// applications.
2323
#[must_use = "streams do nothing unless polled"]
2424
pub struct Body {

src/body/to_bytes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::HttpBody;
55
/// Concatenate the buffers from a body into a single `Bytes` asynchronously.
66
///
77
/// This may require copying the data into a single buffer. If you don't need
8-
/// a contiguous buffer, prefer the [`aggregate`](crate::body::aggregate)
8+
/// a contiguous buffer, prefer the [`aggregate`](crate::body::aggregate())
99
/// function.
1010
pub async fn to_bytes<T>(body: T) -> Result<Bytes, T::Error>
1111
where

src/client/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
//! println!("status: {}", res.status());
4141
//!
4242
//! // Concatenate the body stream into a single buffer...
43-
//! let buf = hyper::body::to_bytes(res.into_body()).await?;
43+
//! let buf = hyper::body::to_bytes(res).await?;
4444
//!
4545
//! println!("body: {:?}", buf);
4646
//! # Ok(())

tests/client.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ macro_rules! test {
146146
);
147147
)*
148148

149-
let body = rt.block_on(concat(res.into_body()))
149+
let body = rt.block_on(concat(res))
150150
.expect("body concat wait");
151151

152152
let expected_res_body = Option::<&[u8]>::from($response_body)
@@ -1065,7 +1065,7 @@ mod dispatch_impl {
10651065
.request(req)
10661066
.and_then(move |res| {
10671067
assert_eq!(res.status(), hyper::StatusCode::OK);
1068-
concat(res.into_body())
1068+
concat(res)
10691069
})
10701070
.map_ok(|_| ())
10711071
};
@@ -1128,7 +1128,7 @@ mod dispatch_impl {
11281128
.unwrap();
11291129
let res = client.request(req).and_then(move |res| {
11301130
assert_eq!(res.status(), hyper::StatusCode::OK);
1131-
concat(res.into_body())
1131+
concat(res)
11321132
});
11331133
let rx = rx1.expect("thread panicked");
11341134

@@ -1296,7 +1296,7 @@ mod dispatch_impl {
12961296
.unwrap();
12971297
let res = client.request(req).and_then(move |res| {
12981298
assert_eq!(res.status(), hyper::StatusCode::OK);
1299-
concat(res.into_body())
1299+
concat(res)
13001300
});
13011301
let rx = rx1.expect("thread panicked");
13021302

@@ -1342,7 +1342,7 @@ mod dispatch_impl {
13421342
.unwrap();
13431343
let res = client.request(req).and_then(move |res| {
13441344
assert_eq!(res.status(), hyper::StatusCode::OK);
1345-
concat(res.into_body())
1345+
concat(res)
13461346
});
13471347
let rx = rx1.expect("thread panicked");
13481348

@@ -2098,7 +2098,7 @@ mod conn {
20982098

20992099
let res = client.send_request(req).and_then(move |res| {
21002100
assert_eq!(res.status(), hyper::StatusCode::OK);
2101-
concat(res.into_body())
2101+
concat(res)
21022102
});
21032103
let rx = rx1.expect("thread panicked");
21042104
let rx = rx.then(|_| tokio::time::delay_for(Duration::from_millis(200)));
@@ -2144,7 +2144,7 @@ mod conn {
21442144

21452145
let res = client.send_request(req).and_then(move |res| {
21462146
assert_eq!(res.status(), hyper::StatusCode::OK);
2147-
concat(res.into_body())
2147+
concat(res)
21482148
});
21492149
let rx = rx1.expect("thread panicked");
21502150
let rx = rx.then(|_| tokio::time::delay_for(Duration::from_millis(200)));
@@ -2184,7 +2184,7 @@ mod conn {
21842184
.unwrap();
21852185
let res1 = client.send_request(req).and_then(move |res| {
21862186
assert_eq!(res.status(), hyper::StatusCode::OK);
2187-
concat(res.into_body())
2187+
concat(res)
21882188
});
21892189

21902190
// pipelined request will hit NotReady, and thus should return an Error::Cancel
@@ -2258,7 +2258,7 @@ mod conn {
22582258
let res = client.send_request(req).and_then(move |res| {
22592259
assert_eq!(res.status(), hyper::StatusCode::SWITCHING_PROTOCOLS);
22602260
assert_eq!(res.headers()["Upgrade"], "foobar");
2261-
concat(res.into_body())
2261+
concat(res)
22622262
});
22632263

22642264
let rx = rx1.expect("thread panicked");
@@ -2348,7 +2348,7 @@ mod conn {
23482348
.send_request(req)
23492349
.and_then(move |res| {
23502350
assert_eq!(res.status(), hyper::StatusCode::OK);
2351-
concat(res.into_body())
2351+
concat(res)
23522352
})
23532353
.map_ok(|body| {
23542354
assert_eq!(body.as_ref(), b"");

tests/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ impl tower_service::Service<Request<Body>> for TestService {
18871887
let replies = self.reply.clone();
18881888

18891889
Box::pin(async move {
1890-
while let Some(chunk) = req.body_mut().data().await {
1890+
while let Some(chunk) = req.data().await {
18911891
match chunk {
18921892
Ok(chunk) => {
18931893
tx.send(Msg::Chunk(chunk.to_vec())).unwrap();

tests/support/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ async fn async_test(cfg: __TestConfig) {
355355
func(&req.headers());
356356
}
357357
let sbody = sreq.body;
358-
hyper::body::to_bytes(req.into_body()).map_ok(move |body| {
358+
hyper::body::to_bytes(req).map_ok(move |body| {
359359
assert_eq!(body.as_ref(), sbody.as_slice(), "client body");
360360

361361
let mut res = Response::builder()
@@ -410,7 +410,7 @@ async fn async_test(cfg: __TestConfig) {
410410
for func in &cheaders {
411411
func(&res.headers());
412412
}
413-
hyper::body::to_bytes(res.into_body())
413+
hyper::body::to_bytes(res)
414414
})
415415
.map_ok(move |body| {
416416
assert_eq!(body.as_ref(), cbody.as_slice(), "server body");

0 commit comments

Comments
 (0)