From d943ba6d81ba932423f55a086ffd9c9476a5b2db Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 3 Mar 2022 22:50:31 +0100 Subject: [PATCH] Replace `HasRoutes` with `Into` (#819) * Move `HasRoutes` into axum * fix doc test * Just use `Into` --- axum-extra/CHANGELOG.md | 4 +++ axum-extra/src/routing/mod.rs | 48 +----------------------------- axum-extra/src/routing/resource.rs | 12 ++++---- axum/CHANGELOG.md | 2 ++ axum/src/routing/mod.rs | 7 +++-- 5 files changed, 17 insertions(+), 56 deletions(-) diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 1319fec8be..de737c3f58 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -12,12 +12,16 @@ and this project adheres to [Semantic Versioning]. - **breaking:** `CachedRejection` has been removed ([#699]) - **breaking:** ` as FromRequest>::Rejection` is now `T::Rejection`. ([#699]) - **breaking:** `middleware::from_fn` has been moved into the main axum crate ([#719]) +- **breaking:** `HasRoutes` has been removed. `Router::merge` now accepts `Into` ([#819]) +- **breaking:** `RouterExt::with` method has been removed. Use `Router::merge` instead. It works + identically ([#819]) [#666]: https://github.com/tokio-rs/axum/pull/666 [#699]: https://github.com/tokio-rs/axum/pull/699 [#719]: https://github.com/tokio-rs/axum/pull/719 [#756]: https://github.com/tokio-rs/axum/pull/756 [#790]: https://github.com/tokio-rs/axum/pull/790 +[#819]: https://github.com/tokio-rs/axum/pull/819 # 0.1.2 (13. January, 2022) diff --git a/axum-extra/src/routing/mod.rs b/axum-extra/src/routing/mod.rs index 884cc48461..51fd60c598 100644 --- a/axum-extra/src/routing/mod.rs +++ b/axum-extra/src/routing/mod.rs @@ -1,6 +1,6 @@ //! Additional types for defining routes. -use axum::{body::Body, handler::Handler, Router}; +use axum::{handler::Handler, Router}; mod resource; @@ -17,31 +17,6 @@ pub use self::typed::{FirstElementIs, TypedPath}; /// Extension trait that adds additional methods to [`Router`]. pub trait RouterExt: sealed::Sealed { - /// Add the routes from `T`'s [`HasRoutes::routes`] to this router. - /// - /// # Example - /// - /// Using [`Resource`] which implements [`HasRoutes`]: - /// - /// ```rust - /// use axum::{Router, routing::get}; - /// use axum_extra::routing::{RouterExt, Resource}; - /// - /// let app = Router::new() - /// .with( - /// Resource::named("users") - /// .index(|| async {}) - /// .create(|| async {}) - /// ) - /// .with( - /// Resource::named("teams").index(|| async {}) - /// ); - /// # let _: Router = app; - /// ``` - fn with(self, routes: T) -> Self - where - T: HasRoutes; - /// Add a typed `GET` route to the router. /// /// The path will be inferred from the first argument to the handler function which must @@ -151,13 +126,6 @@ impl RouterExt for Router where B: axum::body::HttpBody + Send + 'static, { - fn with(self, routes: T) -> Self - where - T: HasRoutes, - { - self.merge(routes.routes()) - } - #[cfg(feature = "typed-routing")] fn typed_get(self, handler: H) -> Self where @@ -239,20 +207,6 @@ where } } -/// Trait for things that can provide routes. -/// -/// Used with [`RouterExt::with`]. -pub trait HasRoutes { - /// Get the routes. - fn routes(self) -> Router; -} - -impl HasRoutes for Router { - fn routes(self) -> Router { - self - } -} - mod sealed { pub trait Sealed {} impl Sealed for axum::Router {} diff --git a/axum-extra/src/routing/resource.rs b/axum-extra/src/routing/resource.rs index 830dc40469..98105facd9 100644 --- a/axum-extra/src/routing/resource.rs +++ b/axum-extra/src/routing/resource.rs @@ -1,4 +1,3 @@ -use super::HasRoutes; use axum::{ body::Body, handler::Handler, @@ -45,7 +44,7 @@ use tower_service::Service; /// Router::new().route("/featured", get(|| async {})), /// ); /// -/// let app = Router::new().with(users); +/// let app = Router::new().merge(users); /// # let _: Router = app; /// ``` #[derive(Debug)] @@ -182,9 +181,9 @@ where } } -impl HasRoutes for Resource { - fn routes(self) -> Router { - self.router +impl From> for Router { + fn from(resource: Resource) -> Self { + resource.router } } @@ -192,7 +191,6 @@ impl HasRoutes for Resource { mod tests { #[allow(unused_imports)] use super::*; - use crate::routing::RouterExt; use axum::{extract::Path, http::Method, Router}; use tower::ServiceExt; @@ -214,7 +212,7 @@ mod tests { Router::new().route("/featured", get(|| async move { "users#featured" })), ); - let mut app = Router::new().with(users); + let mut app = Router::new().merge(users); assert_eq!( call_route(&mut app, Method::GET, "/users").await, diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index a02ac1d7c0..170746828e 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **added:** `Extension<_>` can now be used in tuples for building responses, and will set an extension on the response ([#797]) - **added:** Implement `tower::Layer` for `Extension` ([#801]) +- **changed:** `Router::merge` now accepts `Into` ([#819]) - **breaking:** `sse::Event` now accepts types implementing `AsRef` instead of `Into` as field values. - **breaking:** `sse::Event` now panics if a setter method is called twice instead of silently @@ -80,6 +81,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#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 +[#819]: https://github.com/tokio-rs/axum/pull/819 # 0.4.4 (13. January, 2022) diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index bf2f5663d3..cd916c0aaf 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -245,13 +245,16 @@ where } #[doc = include_str!("../docs/routing/merge.md")] - pub fn merge(mut self, other: Router) -> Self { + pub fn merge(mut self, other: R) -> Self + where + R: Into>, + { let Router { routes, node, fallback, nested_at_root, - } = other; + } = other.into(); for (id, route) in routes { let path = node