mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 18:44:35 +01:00
add doc string to guards
This commit is contained in:
parent
b6b2eadb3a
commit
88e5059910
38
src/guard.rs
38
src/guard.rs
@ -1,4 +1,30 @@
|
||||
//! Route match guards.
|
||||
//!
|
||||
//! Guards are one of the way how actix-web router chooses
|
||||
//! handler service. In essence it just function that accepts
|
||||
//! reference to a `RequestHead` instance and returns boolean.
|
||||
//! It is possible to add guards to *scopes*, *resources*
|
||||
//! and *routes*. Actix provide several guards by default, like various
|
||||
//! http methods, header, etc. To become a guard, type must implement `Guard`
|
||||
//! trait. Simple functions coulds guards as well.
|
||||
//!
|
||||
//! Guard can not modify request object. But it is possible to
|
||||
//! to store extra attributes on a request by using `Extensions` container.
|
||||
//! Extensions container available via `RequestHead::extensions()` method.
|
||||
//!
|
||||
//! ```rust
|
||||
//! use actix_web::{web, http, dev, guard, App, HttpResponse};
|
||||
//!
|
||||
//! fn main() {
|
||||
//! App::new().service(web::resource("/index.html").route(
|
||||
//! web::route()
|
||||
//! .guard(guard::Post())
|
||||
//! .guard(|head: &dev::RequestHead| head.method == http::Method::GET)
|
||||
//! .to(|| HttpResponse::MethodNotAllowed()))
|
||||
//! );
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
#![allow(non_snake_case)]
|
||||
use actix_http::http::{self, header, HttpTryFrom};
|
||||
use actix_http::RequestHead;
|
||||
@ -13,6 +39,18 @@ pub trait Guard {
|
||||
fn check(&self, request: &RequestHead) -> bool;
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct FnGuard<F: Fn(&RequestHead) -> bool + 'static>(F);
|
||||
|
||||
impl<F> Guard for F
|
||||
where
|
||||
F: Fn(&RequestHead) -> bool + 'static,
|
||||
{
|
||||
fn check(&self, head: &RequestHead) -> bool {
|
||||
(*self)(head)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return guard that matches if any of supplied guards.
|
||||
///
|
||||
/// ```rust
|
||||
|
Loading…
Reference in New Issue
Block a user