diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index d1a495fd..a02ac1d7 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **breaking:** `Redirect::found` has been removed ([#800]) - **breaking:** `AddExtensionLayer` has been removed. Use `Extension` instead. It now implements `tower::Layer` ([#807]) +- **breaking:** `AddExtension` has been moved from the root module to `middleware` - **fixed:** Set `Allow` header when responding with `405 Method Not Allowed` ([#733]) - **fixed:** Correctly set the `Content-Length` header for response to `HEAD` requests ([#734]) diff --git a/axum/src/add_extension.rs b/axum/src/add_extension.rs deleted file mode 100644 index 9739eda4..00000000 --- a/axum/src/add_extension.rs +++ /dev/null @@ -1,37 +0,0 @@ -// this is vendored from tower-http to reduce public dependencies - -use http::Request; -use std::task::{Context, Poll}; -use tower_service::Service; - -/// Middleware for adding some shareable value to [request extensions]. -/// -/// See [Sharing state with handlers](index.html#sharing-state-with-handlers) -/// for more details. -/// -/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html -#[derive(Clone, Copy, Debug)] -pub struct AddExtension { - pub(crate) inner: S, - pub(crate) value: T, -} - -impl Service> for AddExtension -where - S: Service>, - T: Clone + Send + Sync + 'static, -{ - type Response = S::Response; - type Error = S::Error; - type Future = S::Future; - - #[inline] - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.inner.poll_ready(cx) - } - - fn call(&mut self, mut req: Request) -> Self::Future { - req.extensions_mut().insert(self.value.clone()); - self.inner.call(req) - } -} diff --git a/axum/src/extension.rs b/axum/src/extension.rs index 8ca50d88..ece4d68e 100644 --- a/axum/src/extension.rs +++ b/axum/src/extension.rs @@ -1,8 +1,15 @@ -use crate::extract::{rejection::*, FromRequest, RequestParts}; -use crate::response::IntoResponseParts; +use crate::{ + extract::{rejection::*, FromRequest, RequestParts}, + response::IntoResponseParts, +}; use async_trait::async_trait; use axum_core::response::{IntoResponse, Response, ResponseParts}; -use std::ops::Deref; +use http::Request; +use std::{ + ops::Deref, + task::{Context, Poll}, +}; +use tower_service::Service; /// Extractor and response for extensions. /// @@ -120,12 +127,44 @@ impl tower_layer::Layer for Extension where T: Clone + Send + Sync + 'static, { - type Service = crate::AddExtension; + type Service = AddExtension; fn layer(&self, inner: S) -> Self::Service { - crate::AddExtension { + AddExtension { inner, value: self.0.clone(), } } } + +/// Middleware for adding some shareable value to [request extensions]. +/// +/// See [Sharing state with handlers](index.html#sharing-state-with-handlers) +/// for more details. +/// +/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html +#[derive(Clone, Copy, Debug)] +pub struct AddExtension { + pub(crate) inner: S, + pub(crate) value: T, +} + +impl Service> for AddExtension +where + S: Service>, + T: Clone + Send + Sync + 'static, +{ + type Response = S::Response; + type Error = S::Error; + type Future = S::Future; + + #[inline] + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + self.inner.poll_ready(cx) + } + + fn call(&mut self, mut req: Request) -> Self::Future { + req.extensions_mut().insert(self.value.clone()); + self.inner.call(req) + } +} diff --git a/axum/src/extract/connect_info.rs b/axum/src/extract/connect_info.rs index e5622031..004e1d83 100644 --- a/axum/src/extract/connect_info.rs +++ b/axum/src/extract/connect_info.rs @@ -5,7 +5,7 @@ //! [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info use super::{Extension, FromRequest, RequestParts}; -use crate::AddExtension; +use crate::middleware::AddExtension; use async_trait::async_trait; use hyper::server::conn::AddrStream; use std::{ diff --git a/axum/src/lib.rs b/axum/src/lib.rs index 9f3b02a9..a76a7bad 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -392,7 +392,6 @@ #[macro_use] pub(crate) mod macros; -mod add_extension; mod extension; #[cfg(feature = "json")] mod json; @@ -409,7 +408,6 @@ pub mod routing; #[cfg(test)] mod test_helpers; -pub use add_extension::AddExtension; #[doc(no_inline)] pub use async_trait::async_trait; #[cfg(feature = "headers")] diff --git a/axum/src/middleware/mod.rs b/axum/src/middleware/mod.rs index 6e3905d9..3273b7d4 100644 --- a/axum/src/middleware/mod.rs +++ b/axum/src/middleware/mod.rs @@ -5,6 +5,7 @@ mod from_fn; pub use self::from_fn::{from_fn, FromFn, FromFnLayer, Next}; +pub use crate::extension::AddExtension; pub mod future { //! Future types.