diff --git a/axum-core/CHANGELOG.md b/axum-core/CHANGELOG.md index 99811259..2af6e35b 100644 --- a/axum-core/CHANGELOG.md +++ b/axum-core/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- **added** Implement IntoResponse for &'static [u8; N] and [u8; N] ([#1690]) + +[#1690]: https://github.com/tokio-rs/axum/pull/1690 # 0.3.1 (9. January, 2023) @@ -124,7 +126,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **added:** Add `response::ErrorResponse` and `response::Result` for `IntoResponse`-based error handling ([#921]) -[#921]: https://github.com/tokio-rs/axum/pull/921 +[#921]: https://github.com/tokio-rs/axum/pull/921 # 0.2.2 (19. April, 2022) diff --git a/axum-core/src/response/into_response.rs b/axum-core/src/response/into_response.rs index b17c3623..f19974cf 100644 --- a/axum-core/src/response/into_response.rs +++ b/axum-core/src/response/into_response.rs @@ -348,6 +348,18 @@ impl IntoResponse for &'static [u8] { } } +impl IntoResponse for &'static [u8; N] { + fn into_response(self) -> Response { + self.as_slice().into_response() + } +} + +impl IntoResponse for [u8; N] { + fn into_response(self) -> Response { + self.to_vec().into_response() + } +} + impl IntoResponse for Vec { fn into_response(self) -> Response { Cow::<'static, [u8]>::Owned(self).into_response() diff --git a/axum-macros/tests/debug_handler/fail/wrong_return_type.stderr b/axum-macros/tests/debug_handler/fail/wrong_return_type.stderr index 303ae186..74df8fd0 100644 --- a/axum-macros/tests/debug_handler/fail/wrong_return_type.stderr +++ b/axum-macros/tests/debug_handler/fail/wrong_return_type.stderr @@ -5,6 +5,7 @@ error[E0277]: the trait bound `bool: IntoResponse` is not satisfied | ^^^^ the trait `IntoResponse` is not implemented for `bool` | = help: the following other types implement trait `IntoResponse`: + &'static [u8; N] &'static [u8] &'static str () @@ -12,8 +13,7 @@ error[E0277]: the trait bound `bool: IntoResponse` is not satisfied (Response<()>, T1, R) (Response<()>, T1, T2, R) (Response<()>, T1, T2, T3, R) - (Response<()>, T1, T2, T3, T4, R) - and 120 others + and 122 others note: required by a bound in `__axum_macros_check_handler_into_response::{closure#0}::check` --> tests/debug_handler/fail/wrong_return_type.rs:4:23 | diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 151f63e4..a7b7e4f1 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- - **added** Implement IntoResponse for &'static [u8; N] and [u8; N] ([#1690]) + +[#1690]: https://github.com/tokio-rs/axum/pull/1690 # 0.6.2 (9. January, 2023) @@ -48,7 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ```rust // this time without a fallback let api_router = Router::new().route("/users", get(|| { ... })); - + let app = Router::new() .nest("/api", api_router) // `api_router` will inherit this fallback @@ -198,9 +200,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 let app = Router::new() .route("/", get(handler)) .layer(Extension(AppState {})); - + async fn handler(Extension(app_state): Extension) {} - + #[derive(Clone)] struct AppState {} ``` @@ -213,9 +215,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 let app = Router::new() .route("/", get(handler)) .with_state(AppState {}); - + async fn handler(State(app_state): State) {} - + #[derive(Clone)] struct AppState {} ``` @@ -232,22 +234,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 }; let app = Router::new().route("/", get(handler)).with_state(state); - + async fn handler( State(client): State, State(database): State, ) {} - + // the derive requires enabling the "macros" feature #[derive(Clone, FromRef)] struct AppState { client: HttpClient, database: Database, } - + #[derive(Clone)] struct HttpClient {} - + #[derive(Clone)] struct Database {} ``` @@ -301,7 +303,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 B: Send, { type Rejection = StatusCode; - + async fn from_request(req: &mut RequestParts) -> Result { // ... } @@ -326,7 +328,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 S: Send + Sync, { type Rejection = StatusCode; - + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { // ... } @@ -340,7 +342,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 B: Send + 'static, { type Rejection = StatusCode; - + async fn from_request(req: Request, state: &S) -> Result { // ... } @@ -775,9 +777,9 @@ Yanked, as it didn't compile in release mode. let app = Router::new() .route("/", get(handler)) .layer(Extension(AppState {})); - + async fn handler(Extension(app_state): Extension) {} - + #[derive(Clone)] struct AppState {} ``` @@ -789,9 +791,9 @@ Yanked, as it didn't compile in release mode. let app = Router::with_state(AppState {}) .route("/", get(handler)); - + async fn handler(State(app_state): State) {} - + #[derive(Clone)] struct AppState {} ``` @@ -808,30 +810,30 @@ Yanked, as it didn't compile in release mode. }; let app = Router::with_state(state).route("/", get(handler)); - + async fn handler( State(client): State, State(database): State, ) {} - + #[derive(Clone)] struct AppState { client: HttpClient, database: Database, } - + #[derive(Clone)] struct HttpClient {} - + impl FromRef for HttpClient { fn from_ref(state: &AppState) -> Self { state.client.clone() } } - + #[derive(Clone)] struct Database {} - + impl FromRef for Database { fn from_ref(state: &AppState) -> Self { state.database.clone() @@ -887,7 +889,7 @@ Yanked, as it didn't compile in release mode. B: Send, { type Rejection = StatusCode; - + async fn from_request(req: &mut RequestParts) -> Result { // ... } @@ -912,7 +914,7 @@ Yanked, as it didn't compile in release mode. S: Send + Sync, { type Rejection = StatusCode; - + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { // ... } @@ -926,7 +928,7 @@ Yanked, as it didn't compile in release mode. B: Send + 'static, { type Rejection = StatusCode; - + async fn from_request(req: Request, state: &S) -> Result { // ... }