From 554e3a0ad4587f74665c97412a16f01d9e2cc397 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Wed, 13 Oct 2021 12:21:22 +0200 Subject: [PATCH] Remove `routing::Layered` (#383) --- CHANGELOG.md | 2 ++ src/handler/mod.rs | 2 +- src/routing/mod.rs | 65 ++++------------------------------------------ 3 files changed, 8 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 023bb58c..d7040dbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **breaking:** Added feature flags for HTTP1 and JSON. This enables removing a few dependencies if your app only uses HTTP2 or doesn't use JSON. Its only a breaking change if you depend on axum with `default_features = false`. ([#286]) +- **breaking:** Remove `routing::Layered` as it didn't actually do anything and + thus wasn't necessary [#339]: https://github.com/tokio-rs/axum/pull/339 [#286]: https://github.com/tokio-rs/axum/pull/286 diff --git a/src/handler/mod.rs b/src/handler/mod.rs index d2ecfaa4..2fd0f4c1 100644 --- a/src/handler/mod.rs +++ b/src/handler/mod.rs @@ -241,7 +241,7 @@ pub trait Handler: Clone + Send + Sized + 'static { /// This can be used to add additional processing to a request for a single /// handler. /// - /// Note this differs from [`routing::Layered`](crate::routing::Layered) + /// Note this differs from [`routing::Router::layer`](crate::routing::Router::layer) /// which adds a middleware to a group of routes. /// /// # Example diff --git a/src/routing/mod.rs b/src/routing/mod.rs index 99b5a8d5..350e306a 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -260,13 +260,13 @@ impl Router { ResBody: http_body::Body + Send + Sync + 'static, ResBody::Error: Into, { - self.map(|svc| { + self.layer( ServiceBuilder::new() .layer_fn(BoxRoute) .layer_fn(CloneBoxService::new) .layer(MapResponseBodyLayer::new(box_body)) - .service(svc) - }) + .into_inner(), + ) } /// Apply a [`tower::Layer`] to the router. @@ -336,11 +336,11 @@ impl Router { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` - pub fn layer(self, layer: L) -> Router> + pub fn layer(self, layer: L) -> Router where L: Layer, { - self.map(|svc| Layered::new(layer.layer(svc))) + self.map(|svc| layer.layer(svc)) } /// Convert this router into a [`MakeService`], that is a [`Service`] who's @@ -891,58 +891,6 @@ where } } -/// A [`Service`] created from a router by applying a Tower middleware. -/// -/// Created with [`Router::layer`]. See that method for more details. -pub struct Layered { - inner: S, -} - -impl Layered { - fn new(inner: S) -> Self { - Self { inner } - } -} - -impl Clone for Layered -where - S: Clone, -{ - fn clone(&self) -> Self { - Self::new(self.inner.clone()) - } -} - -impl fmt::Debug for Layered -where - S: fmt::Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Layered") - .field("inner", &self.inner) - .finish() - } -} - -impl Service for Layered -where - S: Service, -{ - 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) - } - - #[inline] - fn call(&mut self, req: R) -> Self::Future { - self.inner.call(req) - } -} - /// A [`Service`] that has been nested inside a router at some path. /// /// Created with [`Router::nest`]. @@ -1144,9 +1092,6 @@ mod tests { assert_send::>(); assert_sync::>(); - assert_send::>(); - assert_sync::>(); - assert_send::>(); assert_sync::>();