Create BytesMut for Json with initial capacity (#1196)

* Create BytesMut for Json with initial capacity

* Add a comment explaining BytesMut initial capacity

* Update link to serde_json::to_vec function to docs.rs
This commit is contained in:
Ryan Fowler 2022-07-26 10:45:58 -04:00 committed by GitHub
parent 3bcc8fb07d
commit 7e7a2f2058
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -186,7 +186,9 @@ where
T: Serialize,
{
fn into_response(self) -> Response {
let mut buf = BytesMut::new().writer();
// Use a small initial capacity of 128 bytes like serde_json::to_vec
// https://docs.rs/serde_json/1.0.82/src/serde_json/ser.rs.html#2189
let mut buf = BytesMut::with_capacity(128).writer();
match serde_json::to_writer(&mut buf, &self.0) {
Ok(()) => (
[(