From e09cc593655de82d01971b55130a83842ac46684 Mon Sep 17 00:00:00 2001
From: Mohammed Alotaibi
Date: Sat, 11 Jan 2025 21:50:32 +0300
Subject: [PATCH] Make `status` a const function in rejection handling (#3168)
Signed-off-by: Awiteb
---
axum-core/CHANGELOG.md | 6 ++++++
axum-core/src/macros.rs | 6 +++---
axum-extra/CHANGELOG.md | 3 +++
axum/CHANGELOG.md | 3 +++
axum/src/extract/path/mod.rs | 4 ++--
5 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/axum-core/CHANGELOG.md b/axum-core/CHANGELOG.md
index f64b00e3ce..3c53d1dcb3 100644
--- a/axum-core/CHANGELOG.md
+++ b/axum-core/CHANGELOG.md
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased
+- **change:** Make the `status` function for rejections generated by the
+ `__define_rejection` and `__composite_rejection` macros a `const` function
+ ([#3168])
+
+[#3168]: https://github.com/tokio-rs/axum/pull/3168
+
# 0.5.0
## since rc.1
diff --git a/axum-core/src/macros.rs b/axum-core/src/macros.rs
index 8f2762486e..7311fdfba3 100644
--- a/axum-core/src/macros.rs
+++ b/axum-core/src/macros.rs
@@ -54,7 +54,7 @@ macro_rules! __define_rejection {
}
/// Get the status code used for this rejection.
- pub fn status(&self) -> http::StatusCode {
+ pub const fn status(&self) -> http::StatusCode {
http::StatusCode::$status
}
}
@@ -111,7 +111,7 @@ macro_rules! __define_rejection {
}
/// Get the status code used for this rejection.
- pub fn status(&self) -> http::StatusCode {
+ pub const fn status(&self) -> http::StatusCode {
http::StatusCode::$status
}
}
@@ -188,7 +188,7 @@ macro_rules! __composite_rejection {
}
/// Get the status code used for this rejection.
- pub fn status(&self) -> http::StatusCode {
+ pub const fn status(&self) -> http::StatusCode {
match self {
$(
Self::$variant(inner) => inner.status(),
diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md
index a100175057..209cdca734 100644
--- a/axum-extra/CHANGELOG.md
+++ b/axum-extra/CHANGELOG.md
@@ -10,8 +10,11 @@ and this project adheres to [Semantic Versioning].
- **breaking:** Remove unused `async-stream` feature, which was accidentally
introduced as an implicit feature through an optional dependency which was no
longer being used ([#3145])
+- **change:** Make the `status` function of rejections a `const` function, such
+ as `FormRejection`, `QueryRejection` and `MultipartRejection` ([#3168])
[#3145]: https://github.com/tokio-rs/axum/pull/3145
+[#3168]: https://github.com/tokio-rs/axum/pull/3168
# 0.10.0
diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md
index 49339649fe..56aa49be16 100644
--- a/axum/CHANGELOG.md
+++ b/axum/CHANGELOG.md
@@ -9,9 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **added:** Implement `OptionalFromRequest` for `Json` ([#3142])
- **added:** Implement `OptionalFromRequest` for `Extension` ([#3157])
+- **change:** Make the `status` function of rejections a `const` function, such
+ as `JsonRejection`, `QueryRejection` and `PathRejection` ([#3168])
[#3142]: https://github.com/tokio-rs/axum/pull/3142
[#3157]: https://github.com/tokio-rs/axum/pull/3157
+[#3168]: https://github.com/tokio-rs/axum/pull/3168
# 0.8.0
diff --git a/axum/src/extract/path/mod.rs b/axum/src/extract/path/mod.rs
index e883c643eb..6dab914b78 100644
--- a/axum/src/extract/path/mod.rs
+++ b/axum/src/extract/path/mod.rs
@@ -429,7 +429,7 @@ impl FailedToDeserializePathParams {
}
/// Get the status code used for this rejection.
- pub fn status(&self) -> StatusCode {
+ pub const fn status(&self) -> StatusCode {
match self.0.kind {
ErrorKind::Message(_)
| ErrorKind::DeserializeError { .. }
@@ -563,7 +563,7 @@ impl InvalidUtf8InPathParam {
}
/// Get the status code used for this rejection.
- pub fn status(&self) -> StatusCode {
+ pub const fn status(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}