Skip to content

Commit b5bd4e3

Browse files
committed
feat: optimize into_bytes buffer and into_json capacity by body len
Based on https://github.com/http-rs/http-types/pull/506/files by @qwerty2501
2 parents c02acbc + 4c21e64 commit b5bd4e3

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/body.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ impl Body {
173173
/// # Ok(()) }) }
174174
/// ```
175175
pub async fn into_bytes(mut self) -> crate::Result<Vec<u8>> {
176-
let mut buf = Vec::with_capacity(1024);
176+
let len = usize::try_from(self.len().unwrap_or(0)).status(StatusCode::PayloadTooLarge)?;
177+
let mut buf = Vec::with_capacity(len);
177178
self.read_to_end(&mut buf).await.status(StatusCode::UnprocessableEntity)?;
178179
Ok(buf)
179180
}
@@ -273,9 +274,8 @@ impl Body {
273274
/// # Ok(()) }) }
274275
/// ```
275276
#[cfg(feature = "serde")]
276-
pub async fn into_json<T: DeserializeOwned>(mut self) -> crate::Result<T> {
277-
let mut buf = Vec::with_capacity(1024);
278-
self.read_to_end(&mut buf).await?;
277+
pub async fn into_json<T: DeserializeOwned>(self) -> crate::Result<T> {
278+
let buf = self.into_bytes().await?;
279279
serde_json::from_slice(&buf).status(StatusCode::UnprocessableEntity)
280280
}
281281

0 commit comments

Comments
 (0)