From 64a03398d0dc1f4253dc47ac146ab33bf4c68e7a Mon Sep 17 00:00:00 2001 From: Matt Gathu Date: Wed, 16 Sep 2020 06:59:48 +0200 Subject: [PATCH] Implement methods guards using the .or syntax --- CHANGES.md | 3 --- actix-web-codegen/src/route.rs | 14 ++++++++++---- src/guard.rs | 4 ---- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 68d466e41..15d3c81ce 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,9 +1,6 @@ # Changes ## Unreleased - 2020-xx-xx -* Add public constructor for `web::guard::AnyGuard` - -[#1674]: https://github.com/actix/actix-web/pull/1674 ## 3.0.2 - 2020-09-15 diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index e0fbf1657..394ced212 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -261,11 +261,17 @@ impl ToTokens for Route { resource_type, } = self; let resource_name = name.to_string(); - let methods = methods.iter(); + let mut methods = methods.iter(); + + let method_guards = if *guard == GuardType::Multi { + // unwrapping since length is checked to be at least one + let first = methods.next().unwrap(); - let guard_gen = if *guard == GuardType::Multi { quote! { - .guard(actix_web::guard::AnyGuard::new(vec![#(Box::new(actix_web::guard::#methods())),*])) + .guard( + actix_web::guard::Any(actix_web::guard::#first()) + #(.or(actix_web::guard::#methods()))* + ) } } else { quote! { @@ -282,7 +288,7 @@ impl ToTokens for Route { #ast let __resource = actix_web::Resource::new(#path) .name(#resource_name) - #guard_gen + #method_guards #(.guard(actix_web::guard::fn_guard(#guards)))* #(.wrap(#wrappers))* .#resource_type(#name); diff --git a/src/guard.rs b/src/guard.rs index 00f05999f..25284236a 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -104,10 +104,6 @@ pub fn Any(guard: F) -> AnyGuard { pub struct AnyGuard(Vec>); impl AnyGuard { - /// Create AnyGuard from a vector of Guards - pub fn new(guards: Vec>) -> Self { - AnyGuard(guards) - } /// Add guard to a list of guards to check pub fn or(mut self, guard: F) -> Self { self.0.push(Box::new(guard));