From e8bc3f5082c77d722d1b2e249178672b348c88fb Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Fri, 20 Aug 2021 19:51:29 +0200 Subject: [PATCH] Further compile time improvements (#220) This improves compiles further when using lots of nested routes. Such as the example posted [here](https://github.com/tokio-rs/axum/issues/200#issuecomment-902541073). It seems rustc is really slow at checking bounds on these kinds of intermediate builder methods. Should probably file an issue about that. --- src/routing/mod.rs | 10 ++-------- src/tests/handle_error.rs | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/routing/mod.rs b/src/routing/mod.rs index b5228651..3e4a8ea3 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -119,10 +119,7 @@ impl Router { /// # Panics /// /// Panics if `description` doesn't start with `/`. - pub fn route(self, description: &str, svc: T) -> Router> - where - T: Service> + Clone, - { + pub fn route(self, description: &str, svc: T) -> Router> { self.map(|fallback| Route { pattern: PathPattern::new(description), svc, @@ -207,10 +204,7 @@ impl Router { /// If necessary you can use [`Router::boxed`] to box a group of routes /// making the type easier to name. This is sometimes useful when working with /// `nest`. - pub fn nest(self, description: &str, svc: T) -> Router> - where - T: Service> + Clone, - { + pub fn nest(self, description: &str, svc: T) -> Router> { self.map(|fallback| Nested { pattern: PathPattern::new(description), svc, diff --git a/src/tests/handle_error.rs b/src/tests/handle_error.rs index 7bf907c8..cc93fe25 100644 --- a/src/tests/handle_error.rs +++ b/src/tests/handle_error.rs @@ -132,24 +132,26 @@ async fn handler_multiple_methods_last() { #[test] fn service_propagates_errors() { - let app = Router::new().route::<_, Body>("/echo", service::post(Svc)); + let app = Router::new().route("/echo", service::post::<_, Body>(Svc)); check_make_svc::<_, _, _, hyper::Error>(app.into_make_service()); } #[test] fn service_nested_propagates_errors() { - let app = - Router::new().route::<_, Body>("/echo", Router::new().nest("/foo", service::post(Svc))); + let app = Router::new().route( + "/echo", + Router::new().nest("/foo", service::post::<_, Body>(Svc)), + ); check_make_svc::<_, _, _, hyper::Error>(app.into_make_service()); } #[test] fn service_handle_on_method() { - let app = Router::new().route::<_, Body>( + let app = Router::new().route( "/echo", - service::get(Svc).handle_error(handle_error::), + service::get::<_, Body>(Svc).handle_error(handle_error::), ); check_make_svc::<_, _, _, Infallible>(app.into_make_service()); @@ -157,9 +159,9 @@ fn service_handle_on_method() { #[test] fn service_handle_on_method_multiple() { - let app = Router::new().route::<_, Body>( + let app = Router::new().route( "/echo", - service::get(Svc) + service::get::<_, Body>(Svc) .post(Svc) .handle_error(handle_error::), ); @@ -170,7 +172,7 @@ fn service_handle_on_method_multiple() { #[test] fn service_handle_on_router() { let app = Router::new() - .route::<_, Body>("/echo", service::get(Svc)) + .route("/echo", service::get::<_, Body>(Svc)) .handle_error(handle_error::); check_make_svc::<_, _, _, Infallible>(app.into_make_service()); @@ -179,7 +181,7 @@ fn service_handle_on_router() { #[test] fn service_handle_on_router_still_impls_routing_dsl() { let app = Router::new() - .route::<_, Body>("/echo", service::get(Svc)) + .route("/echo", service::get::<_, Body>(Svc)) .handle_error(handle_error::) .route("/", get(unit)); @@ -189,7 +191,7 @@ fn service_handle_on_router_still_impls_routing_dsl() { #[test] fn layered() { let app = Router::new() - .route::<_, Body>("/echo", get(unit)) + .route("/echo", get::<_, Body, _>(unit)) .layer(timeout()) .handle_error(handle_error::); @@ -199,7 +201,7 @@ fn layered() { #[tokio::test] // async because of `.boxed()` async fn layered_boxed() { let app = Router::new() - .route::<_, Body>("/echo", get(unit)) + .route("/echo", get::<_, Body, _>(unit)) .layer(timeout()) .boxed() .handle_error(handle_error::);