Implement IntoResponse for [u8; N] and &'static [u8; N] (#1690)

Co-authored-by: David Pedersen <[email protected]>
This commit is contained in:
valkyrie_pilot
2023-01-13 09:12:51 +00:00
committed by GitHub
co-authored by David Pedersen
parent e3aaeb3cb7
commit e4c6d76bca
4 changed files with 46 additions and 30 deletions
+4 -2
View File
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased # 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) # 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 - **added:** Add `response::ErrorResponse` and `response::Result` for
`IntoResponse`-based error handling ([#921]) `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) # 0.2.2 (19. April, 2022)
+12
View File
@@ -348,6 +348,18 @@ impl IntoResponse for &'static [u8] {
} }
} }
impl<const N: usize> IntoResponse for &'static [u8; N] {
fn into_response(self) -> Response {
self.as_slice().into_response()
}
}
impl<const N: usize> IntoResponse for [u8; N] {
fn into_response(self) -> Response {
self.to_vec().into_response()
}
}
impl IntoResponse for Vec<u8> { impl IntoResponse for Vec<u8> {
fn into_response(self) -> Response { fn into_response(self) -> Response {
Cow::<'static, [u8]>::Owned(self).into_response() Cow::<'static, [u8]>::Owned(self).into_response()
@@ -5,6 +5,7 @@ error[E0277]: the trait bound `bool: IntoResponse` is not satisfied
| ^^^^ the trait `IntoResponse` is not implemented for `bool` | ^^^^ the trait `IntoResponse` is not implemented for `bool`
| |
= help: the following other types implement trait `IntoResponse`: = help: the following other types implement trait `IntoResponse`:
&'static [u8; N]
&'static [u8] &'static [u8]
&'static str &'static str
() ()
@@ -12,8 +13,7 @@ error[E0277]: the trait bound `bool: IntoResponse` is not satisfied
(Response<()>, T1, R) (Response<()>, T1, R)
(Response<()>, T1, T2, R) (Response<()>, T1, T2, R)
(Response<()>, T1, T2, T3, R) (Response<()>, T1, T2, T3, R)
(Response<()>, T1, T2, T3, T4, R) and 122 others
and 120 others
note: required by a bound in `__axum_macros_check_handler_into_response::{closure#0}::check` 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 --> tests/debug_handler/fail/wrong_return_type.rs:4:23
| |
+28 -26
View File
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased # 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) # 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 ```rust
// this time without a fallback // this time without a fallback
let api_router = Router::new().route("/users", get(|| { ... })); let api_router = Router::new().route("/users", get(|| { ... }));
let app = Router::new() let app = Router::new()
.nest("/api", api_router) .nest("/api", api_router)
// `api_router` will inherit this fallback // `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() let app = Router::new()
.route("/", get(handler)) .route("/", get(handler))
.layer(Extension(AppState {})); .layer(Extension(AppState {}));
async fn handler(Extension(app_state): Extension<AppState>) {} async fn handler(Extension(app_state): Extension<AppState>) {}
#[derive(Clone)] #[derive(Clone)]
struct AppState {} struct AppState {}
``` ```
@@ -213,9 +215,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
let app = Router::new() let app = Router::new()
.route("/", get(handler)) .route("/", get(handler))
.with_state(AppState {}); .with_state(AppState {});
async fn handler(State(app_state): State<AppState>) {} async fn handler(State(app_state): State<AppState>) {}
#[derive(Clone)] #[derive(Clone)]
struct AppState {} 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); let app = Router::new().route("/", get(handler)).with_state(state);
async fn handler( async fn handler(
State(client): State<HttpClient>, State(client): State<HttpClient>,
State(database): State<Database>, State(database): State<Database>,
) {} ) {}
// the derive requires enabling the "macros" feature // the derive requires enabling the "macros" feature
#[derive(Clone, FromRef)] #[derive(Clone, FromRef)]
struct AppState { struct AppState {
client: HttpClient, client: HttpClient,
database: Database, database: Database,
} }
#[derive(Clone)] #[derive(Clone)]
struct HttpClient {} struct HttpClient {}
#[derive(Clone)] #[derive(Clone)]
struct Database {} struct Database {}
``` ```
@@ -301,7 +303,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
B: Send, B: Send,
{ {
type Rejection = StatusCode; type Rejection = StatusCode;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> { async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
// ... // ...
} }
@@ -326,7 +328,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
S: Send + Sync, S: Send + Sync,
{ {
type Rejection = StatusCode; type Rejection = StatusCode;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
// ... // ...
} }
@@ -340,7 +342,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
B: Send + 'static, B: Send + 'static,
{ {
type Rejection = StatusCode; type Rejection = StatusCode;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
// ... // ...
} }
@@ -775,9 +777,9 @@ Yanked, as it didn't compile in release mode.
let app = Router::new() let app = Router::new()
.route("/", get(handler)) .route("/", get(handler))
.layer(Extension(AppState {})); .layer(Extension(AppState {}));
async fn handler(Extension(app_state): Extension<AppState>) {} async fn handler(Extension(app_state): Extension<AppState>) {}
#[derive(Clone)] #[derive(Clone)]
struct AppState {} struct AppState {}
``` ```
@@ -789,9 +791,9 @@ Yanked, as it didn't compile in release mode.
let app = Router::with_state(AppState {}) let app = Router::with_state(AppState {})
.route("/", get(handler)); .route("/", get(handler));
async fn handler(State(app_state): State<AppState>) {} async fn handler(State(app_state): State<AppState>) {}
#[derive(Clone)] #[derive(Clone)]
struct AppState {} struct AppState {}
``` ```
@@ -808,30 +810,30 @@ Yanked, as it didn't compile in release mode.
}; };
let app = Router::with_state(state).route("/", get(handler)); let app = Router::with_state(state).route("/", get(handler));
async fn handler( async fn handler(
State(client): State<HttpClient>, State(client): State<HttpClient>,
State(database): State<Database>, State(database): State<Database>,
) {} ) {}
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
client: HttpClient, client: HttpClient,
database: Database, database: Database,
} }
#[derive(Clone)] #[derive(Clone)]
struct HttpClient {} struct HttpClient {}
impl FromRef<AppState> for HttpClient { impl FromRef<AppState> for HttpClient {
fn from_ref(state: &AppState) -> Self { fn from_ref(state: &AppState) -> Self {
state.client.clone() state.client.clone()
} }
} }
#[derive(Clone)] #[derive(Clone)]
struct Database {} struct Database {}
impl FromRef<AppState> for Database { impl FromRef<AppState> for Database {
fn from_ref(state: &AppState) -> Self { fn from_ref(state: &AppState) -> Self {
state.database.clone() state.database.clone()
@@ -887,7 +889,7 @@ Yanked, as it didn't compile in release mode.
B: Send, B: Send,
{ {
type Rejection = StatusCode; type Rejection = StatusCode;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> { async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
// ... // ...
} }
@@ -912,7 +914,7 @@ Yanked, as it didn't compile in release mode.
S: Send + Sync, S: Send + Sync,
{ {
type Rejection = StatusCode; type Rejection = StatusCode;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
// ... // ...
} }
@@ -926,7 +928,7 @@ Yanked, as it didn't compile in release mode.
B: Send + 'static, B: Send + 'static,
{ {
type Rejection = StatusCode; type Rejection = StatusCode;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
// ... // ...
} }