Skip to content

Commit

Permalink
feat: optimize into_bytes buffer and into_json capacity by body len
Browse files Browse the repository at this point in the history
  • Loading branch information
OneOfOne committed Apr 7, 2024
2 parents c02acbc + 4c21e64 commit b5bd4e3
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ impl Body {
/// # Ok(()) }) }
/// ```
pub async fn into_bytes(mut self) -> crate::Result<Vec<u8>> {
let mut buf = Vec::with_capacity(1024);
let len = usize::try_from(self.len().unwrap_or(0)).status(StatusCode::PayloadTooLarge)?;
let mut buf = Vec::with_capacity(len);
self.read_to_end(&mut buf).await.status(StatusCode::UnprocessableEntity)?;
Ok(buf)
}
Expand Down Expand Up @@ -273,9 +274,8 @@ impl Body {
/// # Ok(()) }) }
/// ```
#[cfg(feature = "serde")]
pub async fn into_json<T: DeserializeOwned>(mut self) -> crate::Result<T> {
let mut buf = Vec::with_capacity(1024);
self.read_to_end(&mut buf).await?;
pub async fn into_json<T: DeserializeOwned>(self) -> crate::Result<T> {
let buf = self.into_bytes().await?;
serde_json::from_slice(&buf).status(StatusCode::UnprocessableEntity)
}

Expand Down

0 comments on commit b5bd4e3

Please sign in to comment.