From 2de202da40410ddd55c9b350c76d3e7391cf5fcb Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 1 Mar 2022 13:59:43 +0100 Subject: [PATCH] Remove `AddExtensionLayer` (#807) * Remove `AddExtensionLayer` Its deprecated on 0.4.x so we can remove it from `main`. * changelog --- axum/CHANGELOG.md | 3 +++ axum/src/add_extension.rs | 40 ------------------------------------- axum/src/docs/extract.md | 4 ++-- axum/src/docs/middleware.md | 16 +++++++-------- axum/src/lib.rs | 2 +- 5 files changed, 14 insertions(+), 51 deletions(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 407d7805..d1a495fd 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -56,6 +56,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `MatchedPathRejection` - `WebSocketUpgradeRejection` - **breaking:** `Redirect::found` has been removed ([#800]) +- **breaking:** `AddExtensionLayer` has been removed. Use `Extension` instead. It now implements + `tower::Layer` ([#807]) - **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]) @@ -76,6 +78,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#797]: https://github.com/tokio-rs/axum/pull/797 [#800]: https://github.com/tokio-rs/axum/pull/800 [#801]: https://github.com/tokio-rs/axum/pull/801 +[#807]: https://github.com/tokio-rs/axum/pull/807 # 0.4.4 (13. January, 2022) diff --git a/axum/src/add_extension.rs b/axum/src/add_extension.rs index 19ee8952..9739eda4 100644 --- a/axum/src/add_extension.rs +++ b/axum/src/add_extension.rs @@ -2,41 +2,8 @@ use http::Request; use std::task::{Context, Poll}; -use tower_layer::Layer; use tower_service::Service; -/// [`Layer`] 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 AddExtensionLayer { - value: T, -} - -impl AddExtensionLayer { - /// Create a new [`AddExtensionLayer`]. - pub fn new(value: T) -> Self { - Self { value } - } -} - -impl Layer for AddExtensionLayer -where - T: Clone, -{ - type Service = AddExtension; - - fn layer(&self, inner: S) -> Self::Service { - AddExtension { - inner, - value: self.value.clone(), - } - } -} - /// Middleware for adding some shareable value to [request extensions]. /// /// See [Sharing state with handlers](index.html#sharing-state-with-handlers) @@ -49,13 +16,6 @@ pub struct AddExtension { pub(crate) value: T, } -impl AddExtension { - /// Create a new [`AddExtensionLayer`]. - pub fn layer(value: T) -> AddExtensionLayer { - AddExtensionLayer::new(value) - } -} - impl Service> for AddExtension where S: Service>, diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index a07541cf..edab8bb1 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -376,7 +376,7 @@ use axum::{ http::StatusCode, response::{IntoResponse, Response}, routing::get, - AddExtensionLayer, Router, + Router, }; #[derive(Clone)] @@ -416,7 +416,7 @@ async fn handler(user: AuthenticatedUser) { let state = State { /* ... */ }; -let app = Router::new().route("/", get(handler)).layer(AddExtensionLayer::new(state)); +let app = Router::new().route("/", get(handler)).layer(Extension(state)); # async { # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); # }; diff --git a/axum/src/docs/middleware.md b/axum/src/docs/middleware.md index 86116953..8037fe47 100644 --- a/axum/src/docs/middleware.md +++ b/axum/src/docs/middleware.md @@ -23,7 +23,7 @@ once, instead of calling `layer` (or `route_layer`) repeatedly: ```rust use axum::{ routing::get, - AddExtensionLayer, + Extension, Router, }; use tower_http::{trace::TraceLayer}; @@ -39,7 +39,7 @@ let app = Router::new() .layer( ServiceBuilder::new() .layer(TraceLayer::new_for_http()) - .layer(AddExtensionLayer::new(State {})) + .layer(Extension(State {})) ); # async { # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); @@ -74,9 +74,9 @@ use axum::{routing::get, Router}; async fn handler() {} -# let layer_one = axum::AddExtensionLayer::new(()); -# let layer_two = axum::AddExtensionLayer::new(()); -# let layer_three = axum::AddExtensionLayer::new(()); +# let layer_one = axum::Extension(()); +# let layer_two = axum::Extension(()); +# let layer_three = axum::Extension(()); # let app = Router::new() .route("/", get(handler)) @@ -130,9 +130,9 @@ use axum::{routing::get, Router}; async fn handler() {} -# let layer_one = axum::AddExtensionLayer::new(()); -# let layer_two = axum::AddExtensionLayer::new(()); -# let layer_three = axum::AddExtensionLayer::new(()); +# let layer_one = axum::Extension(()); +# let layer_two = axum::Extension(()); +# let layer_three = axum::Extension(()); # let app = Router::new() .route("/", get(handler)) diff --git a/axum/src/lib.rs b/axum/src/lib.rs index 5f0b996a..9f3b02a9 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -409,7 +409,7 @@ pub mod routing; #[cfg(test)] mod test_helpers; -pub use add_extension::{AddExtension, AddExtensionLayer}; +pub use add_extension::AddExtension; #[doc(no_inline)] pub use async_trait::async_trait; #[cfg(feature = "headers")]