From 5bb924b3a202589dcdcffc0dd243685bf57725cd Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Fri, 29 Apr 2022 17:28:24 +0200 Subject: [PATCH] Implement `IntoResponse` and `IntoResponseParts` for `http::Extensions` (#975) * Implement `IntoResponse` and `IntoResponseParts` for `http::Extensions` Requires a new release of `http`. * remove http patch * changelog ref --- axum-core/CHANGELOG.md | 4 +++- axum-core/Cargo.toml | 2 +- axum-core/src/response/into_response.rs | 10 +++++++++- axum-core/src/response/into_response_parts.rs | 9 +++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/axum-core/CHANGELOG.md b/axum-core/CHANGELOG.md index f1610ad1..276f70a0 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` and `IntoResponseParts` for `http::Extensions` ([#975]) + +[#975]: https://github.com/tokio-rs/axum/pull/975 # 0.2.3 (25. April, 2022) diff --git a/axum-core/Cargo.toml b/axum-core/Cargo.toml index 647a0eaf..141d3f53 100644 --- a/axum-core/Cargo.toml +++ b/axum-core/Cargo.toml @@ -14,7 +14,7 @@ version = "0.2.3" # remember to also bump the version that axum depends on async-trait = "0.1" bytes = "1.0" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } -http = "0.2" +http = "0.2.7" http-body = "0.4" mime = "0.3.16" diff --git a/axum-core/src/response/into_response.rs b/axum-core/src/response/into_response.rs index c2cdcf81..e14936bb 100644 --- a/axum-core/src/response/into_response.rs +++ b/axum-core/src/response/into_response.rs @@ -3,7 +3,7 @@ use crate::{body, BoxError}; use bytes::{buf::Chain, Buf, Bytes, BytesMut}; use http::{ header::{self, HeaderMap, HeaderName, HeaderValue}, - StatusCode, + Extensions, StatusCode, }; use http_body::{ combinators::{MapData, MapErr}, @@ -384,6 +384,14 @@ impl IntoResponse for HeaderMap { } } +impl IntoResponse for Extensions { + fn into_response(self) -> Response { + let mut res = ().into_response(); + *res.extensions_mut() = self; + res + } +} + impl IntoResponse for [(K, V); N] where K: TryInto, diff --git a/axum-core/src/response/into_response_parts.rs b/axum-core/src/response/into_response_parts.rs index 3018b5e8..b6e838a8 100644 --- a/axum-core/src/response/into_response_parts.rs +++ b/axum-core/src/response/into_response_parts.rs @@ -249,3 +249,12 @@ macro_rules! impl_into_response_parts { } all_the_tuples!(impl_into_response_parts); + +impl IntoResponseParts for Extensions { + type Error = Infallible; + + fn into_response_parts(self, mut res: ResponseParts) -> Result { + res.extensions_mut().extend(self); + Ok(res) + } +}