From 6fcbe4bcdabbc8623bbf8a2676e04f8ef9af292b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 30 Mar 2019 11:33:31 -0700 Subject: [PATCH] add fn_guard --- src/guard.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/guard.rs b/src/guard.rs index f9565d0fb..4dcd7ba81 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -39,15 +39,35 @@ pub trait Guard { fn check(&self, request: &RequestHead) -> bool; } -#[doc(hidden)] -pub struct FnGuard bool + 'static>(F); - -impl Guard for F +/// Return guard that matches if all of the supplied guards. +/// +/// ```rust +/// use actix_web::{guard, web, App, HttpResponse}; +/// +/// fn main() { +/// App::new().service(web::resource("/index.html").route( +/// web::route() +/// .guard( +/// guard::fn_guard(|req| req.headers().contains_key("content-type"))) +/// .to(|| HttpResponse::MethodNotAllowed())) +/// ); +/// } +/// ``` +pub fn fn_guard(f: F) -> impl Guard where - F: Fn(&RequestHead) -> bool + 'static, + F: Fn(&RequestHead) -> bool, +{ + FnGuard(f) +} + +struct FnGuard bool>(F); + +impl Guard for FnGuard +where + F: Fn(&RequestHead) -> bool, { fn check(&self, head: &RequestHead) -> bool { - (*self)(head) + (self.0)(head) } } @@ -93,7 +113,6 @@ impl Guard for AnyGuard { /// Return guard that matches if all of the supplied guards. /// /// ```rust -/// # extern crate actix_web; /// use actix_web::{guard, web, App, HttpResponse}; /// /// fn main() {