mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-13 19:27:53 +01:00
Use named constants for static header values (#535)
Improves performance by moving some computation to compile time.
This commit is contained in:
parent
9a410371a6
commit
2bfd51372e
3 changed files with 25 additions and 25 deletions
|
@ -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"
|
||||
|
|
|
@ -176,6 +176,9 @@ where
|
|||
type BodyError = Infallible;
|
||||
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
#[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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -331,23 +331,26 @@ impl IntoResponse for std::borrow::Cow<'static, str> {
|
|||
type BodyError = Infallible;
|
||||
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
#[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<Bytes>;
|
||||
type BodyError = Infallible;
|
||||
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
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<Self::Body> {
|
||||
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<u8> {
|
|||
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
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<Self::Body> {
|
||||
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<Self::Body> {
|
||||
#[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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue