From 2bfd51372e0f62981226ccc86e4ad46e586a788b Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 17 Nov 2021 20:53:07 +0100 Subject: [PATCH] Use named constants for static header values (#535) Improves performance by moving some computation to compile time. --- axum/Cargo.toml | 2 +- axum/src/json.rs | 9 +++++---- axum/src/response/mod.rs | 39 +++++++++++++++++++-------------------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index 011ef0b3..890c0ed8 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -25,7 +25,7 @@ async-trait = "0.1.43" bitflags = "1.0" bytes = "1.0" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } -http = "0.2" +http = "0.2.5" http-body = "0.4.4" hyper = { version = "0.14.14", features = ["server", "tcp", "stream"] } matchit = "0.4.4" diff --git a/axum/src/json.rs b/axum/src/json.rs index 75b9a589..e3534301 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -176,6 +176,9 @@ where type BodyError = Infallible; fn into_response(self) -> Response { + #[allow(clippy::declare_interior_mutable_const)] + const APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json"); + let bytes = match serde_json::to_vec(&self.0) { Ok(res) => res, Err(err) => { @@ -188,10 +191,8 @@ where }; let mut res = Response::new(Full::from(bytes)); - res.headers_mut().insert( - header::CONTENT_TYPE, - HeaderValue::from_static("application/json"), - ); + res.headers_mut() + .insert(header::CONTENT_TYPE, APPLICATION_JSON); res } } diff --git a/axum/src/response/mod.rs b/axum/src/response/mod.rs index b87139ff..b51a77aa 100644 --- a/axum/src/response/mod.rs +++ b/axum/src/response/mod.rs @@ -331,23 +331,26 @@ impl IntoResponse for std::borrow::Cow<'static, str> { type BodyError = Infallible; fn into_response(self) -> Response { + #[allow(clippy::declare_interior_mutable_const)] + const TEXT_PLAIN: HeaderValue = HeaderValue::from_static("text/plain"); + let mut res = Response::new(Full::from(self)); - res.headers_mut() - .insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain")); + res.headers_mut().insert(header::CONTENT_TYPE, TEXT_PLAIN); res } } +#[allow(clippy::declare_interior_mutable_const)] +const APPLICATION_OCTET_STREAM: HeaderValue = HeaderValue::from_static("application/octet-stream"); + impl IntoResponse for Bytes { type Body = Full; type BodyError = Infallible; fn into_response(self) -> Response { let mut res = Response::new(Full::from(self)); - res.headers_mut().insert( - header::CONTENT_TYPE, - HeaderValue::from_static("application/octet-stream"), - ); + res.headers_mut() + .insert(header::CONTENT_TYPE, APPLICATION_OCTET_STREAM); res } } @@ -358,10 +361,8 @@ impl IntoResponse for &'static [u8] { fn into_response(self) -> Response { let mut res = Response::new(Full::from(self)); - res.headers_mut().insert( - header::CONTENT_TYPE, - HeaderValue::from_static("application/octet-stream"), - ); + res.headers_mut() + .insert(header::CONTENT_TYPE, APPLICATION_OCTET_STREAM); res } } @@ -372,10 +373,8 @@ impl IntoResponse for Vec { fn into_response(self) -> Response { let mut res = Response::new(Full::from(self)); - res.headers_mut().insert( - header::CONTENT_TYPE, - HeaderValue::from_static("application/octet-stream"), - ); + res.headers_mut() + .insert(header::CONTENT_TYPE, APPLICATION_OCTET_STREAM); res } } @@ -386,10 +385,8 @@ impl IntoResponse for std::borrow::Cow<'static, [u8]> { fn into_response(self) -> Response { let mut res = Response::new(Full::from(self)); - res.headers_mut().insert( - header::CONTENT_TYPE, - HeaderValue::from_static("application/octet-stream"), - ); + res.headers_mut() + .insert(header::CONTENT_TYPE, APPLICATION_OCTET_STREAM); res } } @@ -471,9 +468,11 @@ where type BodyError = Infallible; fn into_response(self) -> Response { + #[allow(clippy::declare_interior_mutable_const)] + const TEXT_HTML: HeaderValue = HeaderValue::from_static("text/html"); + let mut res = Response::new(self.0.into()); - res.headers_mut() - .insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html")); + res.headers_mut().insert(header::CONTENT_TYPE, TEXT_HTML); res } }