diff --git a/axum-core/src/response/mod.rs b/axum-core/src/response/mod.rs index 534351c2..eb788ecb 100644 --- a/axum-core/src/response/mod.rs +++ b/axum-core/src/response/mod.rs @@ -19,11 +19,6 @@ use http_body::{ }; use std::{borrow::Cow, convert::Infallible, iter}; -mod headers; - -#[doc(inline)] -pub use self::headers::Headers; - /// Type alias for [`http::Response`] whose body type defaults to [`BoxBody`], the most common body /// type used with axum. pub type Response = http::Response; @@ -356,17 +351,10 @@ impl IntoResponse for StatusCode { } } -impl IntoResponse for H -where - H: IntoResponseHeaders, -{ +impl IntoResponse for HeaderMap { fn into_response(self) -> Response { let mut res = Response::new(boxed(Empty::new())); - - if let Err(e) = try_extend_headers(res.headers_mut(), self.into_headers()) { - return e; - } - + *res.headers_mut() = self; res } } diff --git a/axum-debug/tests/fail/wrong_return_type.stderr b/axum-debug/tests/fail/wrong_return_type.stderr index 55e5c269..596ba1d7 100644 --- a/axum-debug/tests/fail/wrong_return_type.stderr +++ b/axum-debug/tests/fail/wrong_return_type.stderr @@ -1,10 +1,9 @@ -error[E0277]: the trait bound `bool: axum_core::response::IntoResponseHeaders` is not satisfied +error[E0277]: the trait bound `bool: IntoResponse` is not satisfied --> tests/fail/wrong_return_type.rs:4:23 | 4 | async fn handler() -> bool { - | ^^^^ the trait `axum_core::response::IntoResponseHeaders` is not implemented for `bool` + | ^^^^ the trait `IntoResponse` is not implemented for `bool` | - = note: required because of the requirements on the impl of `IntoResponse` for `bool` note: required by a bound in `__axum_debug_check_handler_into_response::{closure#0}::check` --> tests/fail/wrong_return_type.rs:4:23 | diff --git a/axum-core/src/response/headers.rs b/axum/src/response/headers.rs similarity index 92% rename from axum-core/src/response/headers.rs rename to axum/src/response/headers.rs index 7176ff98..c12c532e 100644 --- a/axum-core/src/response/headers.rs +++ b/axum/src/response/headers.rs @@ -68,6 +68,19 @@ where } } +impl IntoResponse for Headers +where + H: IntoIterator, + K: TryInto, + K::Error: fmt::Display, + V: TryInto, + V::Error: fmt::Display, +{ + fn into_response(self) -> Response { + (self, ()).into_response() + } +} + #[doc(hidden)] #[derive(Debug)] pub struct IntoIter { @@ -124,7 +137,7 @@ mod tests { let res = (Headers(vec![("user-agent", "axum")]), "foo").into_response(); assert_eq!(res.headers()["user-agent"], "axum"); - let body = crate::body::to_bytes(res.into_body()) + let body = hyper::body::to_bytes(res.into_body()) .now_or_never() .unwrap() .unwrap(); @@ -142,7 +155,7 @@ mod tests { assert_eq!(res.headers()["user-agent"], "axum"); assert_eq!(res.status(), StatusCode::NOT_FOUND); - let body = crate::body::to_bytes(res.into_body()) + let body = hyper::body::to_bytes(res.into_body()) .now_or_never() .unwrap() .unwrap(); diff --git a/axum/src/response/mod.rs b/axum/src/response/mod.rs index fd8f2b8b..7d6540b5 100644 --- a/axum/src/response/mod.rs +++ b/axum/src/response/mod.rs @@ -4,6 +4,7 @@ use crate::body::{Bytes, Full}; use axum_core::body::boxed; use http::{header, HeaderValue}; +mod headers; mod redirect; pub mod sse; @@ -13,10 +14,10 @@ pub mod sse; pub use crate::Json; #[doc(inline)] -pub use axum_core::response::{Headers, IntoResponse, Response}; +pub use axum_core::response::{IntoResponse, IntoResponseHeaders, Response}; #[doc(inline)] -pub use self::{redirect::Redirect, sse::Sse}; +pub use self::{headers::Headers, redirect::Redirect, sse::Sse}; /// An HTML response. ///