mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-30 06:21:07 +00:00
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:
parent
1fe4558362
commit
8dd6070574
@ -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` and `IntoResponseParts` for `http::Extensions` ([#975])
|
||||||
- **added:** Implement `IntoResponse` for `(http::response::Parts, impl IntoResponse)` ([#950])
|
- **added:** Implement `IntoResponse` for `(http::response::Parts, impl IntoResponse)` ([#950])
|
||||||
- **added:** Implement `IntoResponse` for `(http::response::Response<()>, 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
|
[#950]: https://github.com/tokio-rs/axum/pull/950
|
||||||
[#975]: https://github.com/tokio-rs/axum/pull/975
|
[#975]: https://github.com/tokio-rs/axum/pull/975
|
||||||
|
[#980]: https://github.com/tokio-rs/axum/pull/980
|
||||||
|
|
||||||
# 0.2.3 (25. April, 2022)
|
# 0.2.3 (25. April, 2022)
|
||||||
|
|
||||||
|
@ -473,9 +473,44 @@ macro_rules! impl_into_response {
|
|||||||
};
|
};
|
||||||
)*
|
)*
|
||||||
|
|
||||||
let mut res = parts.res;
|
(status, parts.res).into_response()
|
||||||
*res.status_mut() = status;
|
}
|
||||||
res
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,8 +149,12 @@ async fn all_the_things(uri: Uri) -> impl IntoResponse {
|
|||||||
In general you can return tuples like:
|
In general you can return tuples like:
|
||||||
|
|
||||||
- `(StatusCode, impl IntoResponse)`
|
- `(StatusCode, impl IntoResponse)`
|
||||||
|
- `(Parts, impl IntoResponse)`
|
||||||
|
- `(Response<()>, impl IntoResponse)`
|
||||||
- `(T1, .., Tn, impl IntoResponse)` where `T1` to `Tn` all implement [`IntoResponseParts`].
|
- `(T1, .., Tn, impl IntoResponse)` where `T1` to `Tn` all implement [`IntoResponseParts`].
|
||||||
- `(StatusCode, 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
|
This means you cannot accidentally override the status or body as [`IntoResponseParts`] only allows
|
||||||
setting headers and extensions.
|
setting headers and extensions.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user