Skip to content

Commit

Permalink
Further compile time improvements (#220)
Browse files Browse the repository at this point in the history
This improves compiles further when using lots of nested routes. Such as
the example posted
[here](#200 (comment)).

It seems rustc is really slow at checking bounds on these kinds of
intermediate builder methods. Should probably file an issue about that.
  • Loading branch information
davidpdrsn authored Aug 20, 2021
1 parent be61b8c commit e8bc3f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
10 changes: 2 additions & 8 deletions src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ impl<S> Router<S> {
/// # Panics
///
/// Panics if `description` doesn't start with `/`.
pub fn route<T, B>(self, description: &str, svc: T) -> Router<Route<T, S>>
where
T: Service<Request<B>> + Clone,
{
pub fn route<T>(self, description: &str, svc: T) -> Router<Route<T, S>> {
self.map(|fallback| Route {
pattern: PathPattern::new(description),
svc,
Expand Down Expand Up @@ -207,10 +204,7 @@ impl<S> Router<S> {
/// 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<T, B>(self, description: &str, svc: T) -> Router<Nested<T, S>>
where
T: Service<Request<B>> + Clone,
{
pub fn nest<T>(self, description: &str, svc: T) -> Router<Nested<T, S>> {
self.map(|fallback| Nested {
pattern: PathPattern::new(description),
svc,
Expand Down
24 changes: 13 additions & 11 deletions src/tests/handle_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,34 +132,36 @@ 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::<hyper::Error>),
service::get::<_, Body>(Svc).handle_error(handle_error::<hyper::Error>),
);

check_make_svc::<_, _, _, Infallible>(app.into_make_service());
}

#[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::<hyper::Error>),
);
Expand All @@ -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::<hyper::Error>);

check_make_svc::<_, _, _, Infallible>(app.into_make_service());
Expand All @@ -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::<hyper::Error>)
.route("/", get(unit));

Expand All @@ -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::<BoxError>);

Expand All @@ -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::<BoxError>);
Expand Down

0 comments on commit e8bc3f5

Please sign in to comment.