From f1723890cba9d4602268de21533bddad70b91488 Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Sat, 21 Sep 2024 03:02:52 +0800 Subject: [PATCH] Avoid reallocation for String body extraction (#2857) --- axum-core/Cargo.toml | 2 +- axum-core/src/extract/request_parts.rs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/axum-core/Cargo.toml b/axum-core/Cargo.toml index 76dde1bc..c21b218e 100644 --- a/axum-core/Cargo.toml +++ b/axum-core/Cargo.toml @@ -19,7 +19,7 @@ __private_docs = ["dep:tower-http"] [dependencies] async-trait = "0.1.67" -bytes = "1.0" +bytes = "1.2" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } http = "1.0.0" http-body = "1.0.0" diff --git a/axum-core/src/extract/request_parts.rs b/axum-core/src/extract/request_parts.rs index 789b9c1c..5663ddd4 100644 --- a/axum-core/src/extract/request_parts.rs +++ b/axum-core/src/extract/request_parts.rs @@ -137,9 +137,7 @@ where } })?; - let string = std::str::from_utf8(&bytes) - .map_err(InvalidUtf8::from_err)? - .to_owned(); + let string = String::from_utf8(bytes.into()).map_err(InvalidUtf8::from_err)?; Ok(string) }