Skip to content

Commit 7f382ad

Browse files
authored
feat(body): deprecate to_bytes() and aggregate() (#3466)
1 parent ad50497 commit 7f382ad

File tree

9 files changed

+24
-1
lines changed

9 files changed

+24
-1
lines changed

examples/client_json.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
2828
let res = client.get(url).await?;
2929

3030
// asynchronously aggregate the chunks of the body
31+
#[allow(deprecated)]
3132
let body = hyper::body::aggregate(res).await?;
3233

3334
// try to parse as json with serde_json

examples/echo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
3434
// So here we do `.await` on the future, waiting on concatenating the full body,
3535
// then afterwards the content can be reversed. Only then can we return a `Response`.
3636
(&Method::POST, "/echo/reversed") => {
37+
#[allow(deprecated)]
3738
let whole_body = hyper::body::to_bytes(req.into_body()).await?;
3839

3940
let reversed_body = whole_body.iter().rev().cloned().collect::<Vec<u8>>();

examples/params.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +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+
#[allow(deprecated)]
2021
let b = hyper::body::to_bytes(req).await?;
2122
// Parse the request body. form_urlencoded::parse
2223
// always succeeds, but in general parsing may

examples/web_api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +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+
#[allow(deprecated)]
4344
let whole_body = hyper::body::aggregate(req).await?;
4445
// Decode as JSON...
4546
let mut data: serde_json::Value = serde_json::from_reader(whole_body.reader())?;

src/body/aggregate.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ use crate::common::buf::BufList;
1313
/// Care needs to be taken if the remote is untrusted. The function doesn't implement any length
1414
/// checks and an malicious peer might make it consume arbitrary amounts of memory. Checking the
1515
/// `Content-Length` is a possibility, but it is not strictly mandated to be present.
16+
#[cfg_attr(
17+
feature = "deprecated",
18+
deprecated(
19+
note = "This function has been replaced by a method on the `hyper::body::HttpBody` trait. Use `.collect().await?.aggregate()` instead."
20+
)
21+
)]
22+
#[cfg_attr(feature = "deprecated", allow(deprecated))]
1623
pub async fn aggregate<T>(body: T) -> Result<impl Buf, T::Error>
1724
where
1825
T: HttpBody,

src/body/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ pub use bytes::{Buf, Bytes};
1919
pub use http_body::Body as HttpBody;
2020
pub use http_body::SizeHint;
2121

22+
#[cfg_attr(feature = "deprecated", allow(deprecated))]
2223
pub use self::aggregate::aggregate;
2324
pub use self::body::{Body, Sender};
2425
pub(crate) use self::length::DecodedLength;
26+
#[cfg_attr(feature = "deprecated", allow(deprecated))]
2527
pub use self::to_bytes::to_bytes;
2628

2729
mod aggregate;

src/body/to_bytes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ use super::HttpBody;
4444
/// # Ok(())
4545
/// # }
4646
/// ```
47+
#[cfg_attr(
48+
feature = "deprecated",
49+
deprecated(
50+
note = "This function has been replaced by a method on the `hyper::body::HttpBody` trait. Use `.collect().await?.to_bytes()` instead."
51+
)
52+
)]
53+
#[cfg_attr(feature = "deprecated", allow(deprecated))]
4754
pub async fn to_bytes<T>(body: T) -> Result<Bytes, T::Error>
4855
where
4956
T: HttpBody,

tests/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::task::{Context, Poll};
1212
use std::thread;
1313
use std::time::Duration;
1414

15+
#[allow(deprecated)]
1516
use hyper::body::to_bytes as concat;
1617
use hyper::{Body, Client, Method, Request, StatusCode};
1718

@@ -3202,7 +3203,7 @@ mod conn {
32023203
let resp = client.send_request(req).await.expect("send_request");
32033204
assert!(resp.status().is_success());
32043205

3205-
let body = hyper::body::to_bytes(resp.into_body())
3206+
let body = concat(resp.into_body())
32063207
.await
32073208
.expect("get response body with no error");
32083209

tests/support/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ async fn async_test(cfg: __TestConfig) {
374374
func(&req.headers());
375375
}
376376
let sbody = sreq.body;
377+
#[allow(deprecated)]
377378
hyper::body::to_bytes(req).map_ok(move |body| {
378379
assert_eq!(body.as_ref(), sbody.as_slice(), "client body");
379380

@@ -433,6 +434,7 @@ async fn async_test(cfg: __TestConfig) {
433434
for func in &cheaders {
434435
func(&res.headers());
435436
}
437+
#[allow(deprecated)]
436438
hyper::body::to_bytes(res)
437439
})
438440
.map_ok(move |body| {

0 commit comments

Comments
 (0)