mirror of
https://github.com/actix/actix-extras.git
synced 2025-01-22 23:05:56 +01:00
add fn_guard
This commit is contained in:
parent
457b75c995
commit
6fcbe4bcda
33
src/guard.rs
33
src/guard.rs
@ -39,15 +39,35 @@ pub trait Guard {
|
|||||||
fn check(&self, request: &RequestHead) -> bool;
|
fn check(&self, request: &RequestHead) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
/// Return guard that matches if all of the supplied guards.
|
||||||
pub struct FnGuard<F: Fn(&RequestHead) -> bool + 'static>(F);
|
///
|
||||||
|
/// ```rust
|
||||||
impl<F> Guard for F
|
/// 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: F) -> impl Guard
|
||||||
where
|
where
|
||||||
F: Fn(&RequestHead) -> bool + 'static,
|
F: Fn(&RequestHead) -> bool,
|
||||||
|
{
|
||||||
|
FnGuard(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FnGuard<F: Fn(&RequestHead) -> bool>(F);
|
||||||
|
|
||||||
|
impl<F> Guard for FnGuard<F>
|
||||||
|
where
|
||||||
|
F: Fn(&RequestHead) -> bool,
|
||||||
{
|
{
|
||||||
fn check(&self, head: &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.
|
/// Return guard that matches if all of the supplied guards.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # extern crate actix_web;
|
|
||||||
/// use actix_web::{guard, web, App, HttpResponse};
|
/// use actix_web::{guard, web, App, HttpResponse};
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user