not guard constructor is now struct

This commit is contained in:
Rob Ede 2021-12-28 00:35:35 +00:00
parent 0a28772e7b
commit 14ccf67691
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 18 additions and 9 deletions

View File

@ -8,7 +8,8 @@
### Changed
- `Guard` trait now receives a `&GuardContext`. [#2552]
- `guard::fn_guard` functions now receives a `&GuardContext`. [#2552]
- Some guards now return `impl Guard` and their concrete types are made private: `guard::{Not, Header}` and all the method guards. [#2552]
- Some guards now return `impl Guard` and their concrete types are made private: `guard::{Header}` and all the method guards. [#2552]
- The `Not` guard is now generic over the type of guard it wraps. [#2552]
[#2552]: https://github.com/actix/actix-web/pull/2552

View File

@ -253,15 +253,9 @@ impl Guard for AllGuard {
/// .guard(guard::Not(guard::Get()))
/// .to(|| HttpResponse::Ok());
/// ```
#[allow(non_snake_case)]
pub fn Not<G: Guard>(guard: G) -> NotGuard<G> {
NotGuard(guard)
}
pub struct Not<G>(pub G);
#[doc(hidden)]
pub struct NotGuard<G>(G);
impl<G: Guard> Guard for NotGuard<G> {
impl<G: Guard> Guard for Not<G> {
fn check(&self, ctx: &GuardContext<'_>) -> bool {
!self.0.check(ctx)
}
@ -595,4 +589,18 @@ mod tests {
assert!(Any(Get()).or(Trace()).check(&r.guard_ctx()));
assert!(!Any(Get()).or(Get()).check(&r.guard_ctx()));
}
#[test]
fn not_guard_reflexive() {
let req = TestRequest::default().to_srv_request();
let get = Get();
assert!(get.check(&req.guard_ctx()));
let not_get = Not(get);
assert!(!not_get.check(&req.guard_ctx()));
let not_not_get = Not(not_get);
assert!(not_not_get.check(&req.guard_ctx()));
}
}