Implement IntoResponse for (Parts | Request<()>, $(impl IntoResponseParts)+, impl IntoResponse) (#980)

* Implement `IntoResponse for (Parts | Request<()>, $(impl IntoResponseParts)+, impl IntoResponse)`

Fixes #979

* changelog

* docs

* changelog ref
This commit is contained in:
David Pedersen 2022-04-29 22:08:47 +02:00 committed by GitHub
parent 1fe4558362
commit 8dd6070574
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 3 deletions

View file

@ -10,9 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **added:** Implement `IntoResponse` and `IntoResponseParts` for `http::Extensions` ([#975])
- **added:** Implement `IntoResponse` for `(http::response::Parts, impl IntoResponse)` ([#950])
- **added:** Implement `IntoResponse` for `(http::response::Response<()>, impl IntoResponse)` ([#950])
- **added:** Implement `IntoResponse for (Parts | Request<()>, $(impl IntoResponseParts)+, impl IntoResponse)` ([#980])
[#950]: https://github.com/tokio-rs/axum/pull/950
[#975]: https://github.com/tokio-rs/axum/pull/975
[#980]: https://github.com/tokio-rs/axum/pull/980
# 0.2.3 (25. April, 2022)

View file

@ -473,9 +473,44 @@ macro_rules! impl_into_response {
};
)*
let mut res = parts.res;
*res.status_mut() = status;
res
(status, parts.res).into_response()
}
}
#[allow(non_snake_case)]
impl<R, $($ty,)*> IntoResponse for (http::response::Parts, $($ty),*, R)
where
$( $ty: IntoResponseParts, )*
R: IntoResponse,
{
fn into_response(self) -> Response {
let (outer_parts, $($ty),*, res) = self;
let res = res.into_response();
let parts = ResponseParts { res };
$(
let parts = match $ty.into_response_parts(parts) {
Ok(parts) => parts,
Err(err) => {
return err.into_response();
}
};
)*
(outer_parts, parts.res).into_response()
}
}
#[allow(non_snake_case)]
impl<R, $($ty,)*> IntoResponse for (http::response::Response<()>, $($ty),*, R)
where
$( $ty: IntoResponseParts, )*
R: IntoResponse,
{
fn into_response(self) -> Response {
let (template, $($ty),*, res) = self;
let (parts, ()) = template.into_parts();
(parts, $($ty),*, res).into_response()
}
}
}

View file

@ -149,8 +149,12 @@ async fn all_the_things(uri: Uri) -> impl IntoResponse {
In general you can return tuples like:
- `(StatusCode, impl IntoResponse)`
- `(Parts, impl IntoResponse)`
- `(Response<()>, impl IntoResponse)`
- `(T1, .., Tn, impl IntoResponse)` where `T1` to `Tn` all implement [`IntoResponseParts`].
- `(StatusCode, T1, .., Tn, impl IntoResponse)` where `T1` to `Tn` all implement [`IntoResponseParts`].
- `(Parts, T1, .., Tn, impl IntoResponse)` where `T1` to `Tn` all implement [`IntoResponseParts`].
- `(Response<()>, T1, .., Tn, impl IntoResponse)` where `T1` to `Tn` all implement [`IntoResponseParts`].
This means you cannot accidentally override the status or body as [`IntoResponseParts`] only allows
setting headers and extensions.