From 49cfabeaf51a795ee49fc058bd05528225ad5101 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Wed, 5 Jan 2022 07:34:13 +0300 Subject: [PATCH] simplify Resource trait (#2568) Co-authored-by: Rob Ede --- actix-router/CHANGES.md | 4 ++++ actix-router/src/path.rs | 36 ++++++++++++++++++++++++++++--- actix-router/src/resource.rs | 5 ++--- actix-router/src/resource_path.rs | 7 ++++-- actix-router/src/router.rs | 22 ++++++++----------- src/service.rs | 6 ++++-- 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/actix-router/CHANGES.md b/actix-router/CHANGES.md index 66b5379fc..3034ed794 100644 --- a/actix-router/CHANGES.md +++ b/actix-router/CHANGES.md @@ -1,6 +1,10 @@ # Changes ## Unreleased - 2021-xx-xx +- `Resource` trait now have an associated type, `Path`, instead of the generic parameter. [#2568] +- `Resource` is now implemented for `&mut Path<_>` and `RefMut>`. [#2568] + +[#2568]: https://github.com/actix/actix-web/pull/2568 ## 0.5.0-beta.4 - 2022-01-04 diff --git a/actix-router/src/path.rs b/actix-router/src/path.rs index 9af7b0b8b..fc7bb16ac 100644 --- a/actix-router/src/path.rs +++ b/actix-router/src/path.rs @@ -1,5 +1,5 @@ use std::borrow::Cow; -use std::ops::Index; +use std::ops::{DerefMut, Index}; use firestorm::profile_method; use serde::de; @@ -213,8 +213,38 @@ impl Index for Path { } } -impl Resource for Path { - fn resource_path(&mut self) -> &mut Self { +impl Resource for Path { + type Path = T; + + fn resource_path(&mut self) -> &mut Path { self } } + +impl Resource for T +where + T: DerefMut>, + P: ResourcePath, +{ + type Path = P; + + fn resource_path(&mut self) -> &mut Path { + &mut *self + } +} + +#[cfg(test)] +mod tests { + use std::cell::RefCell; + + use super::*; + + #[test] + fn deref_impls() { + let mut foo = Path::new("/foo"); + let _ = (&mut foo).resource_path(); + + let foo = RefCell::new(foo); + let _ = foo.borrow_mut().resource_path(); + } +} diff --git a/actix-router/src/resource.rs b/actix-router/src/resource.rs index f1eb9caf5..d39a6b923 100644 --- a/actix-router/src/resource.rs +++ b/actix-router/src/resource.rs @@ -678,15 +678,14 @@ impl ResourceDef { /// assert!(!try_match(&resource, &mut path)); /// assert_eq!(path.unprocessed(), "/user/admin/stars"); /// ``` - pub fn capture_match_info_fn( + pub fn capture_match_info_fn( &self, resource: &mut R, check_fn: F, user_data: U, ) -> bool where - R: Resource, - T: ResourcePath, + R: Resource, F: FnOnce(&R, U) -> bool, { profile_method!(capture_match_info_fn); diff --git a/actix-router/src/resource_path.rs b/actix-router/src/resource_path.rs index 91a7f2f55..00eb0927c 100644 --- a/actix-router/src/resource_path.rs +++ b/actix-router/src/resource_path.rs @@ -2,8 +2,11 @@ use crate::Path; // TODO: this trait is necessary, document it // see impl Resource for ServiceRequest -pub trait Resource { - fn resource_path(&mut self) -> &mut Path; +pub trait Resource { + /// Type of resource's path returned in `resource_path`. + type Path: ResourcePath; + + fn resource_path(&mut self) -> &mut Path; } pub trait ResourcePath { diff --git a/actix-router/src/router.rs b/actix-router/src/router.rs index fad1a440b..47940708e 100644 --- a/actix-router/src/router.rs +++ b/actix-router/src/router.rs @@ -1,6 +1,6 @@ use firestorm::profile_method; -use crate::{IntoPatterns, Resource, ResourceDef, ResourcePath}; +use crate::{IntoPatterns, Resource, ResourceDef}; #[derive(Debug, Copy, Clone, PartialEq)] pub struct ResourceId(pub u16); @@ -26,10 +26,9 @@ impl Router { } } - pub fn recognize(&self, resource: &mut R) -> Option<(&T, ResourceId)> + pub fn recognize(&self, resource: &mut R) -> Option<(&T, ResourceId)> where - R: Resource

, - P: ResourcePath, + R: Resource, { profile_method!(recognize); @@ -42,10 +41,9 @@ impl Router { None } - pub fn recognize_mut(&mut self, resource: &mut R) -> Option<(&mut T, ResourceId)> + pub fn recognize_mut(&mut self, resource: &mut R) -> Option<(&mut T, ResourceId)> where - R: Resource

, - P: ResourcePath, + R: Resource, { profile_method!(recognize_mut); @@ -58,11 +56,10 @@ impl Router { None } - pub fn recognize_fn(&self, resource: &mut R, check: F) -> Option<(&T, ResourceId)> + pub fn recognize_fn(&self, resource: &mut R, check: F) -> Option<(&T, ResourceId)> where F: Fn(&R, &Option) -> bool, - R: Resource

, - P: ResourcePath, + R: Resource, { profile_method!(recognize_checked); @@ -75,15 +72,14 @@ impl Router { None } - pub fn recognize_mut_fn( + pub fn recognize_mut_fn( &mut self, resource: &mut R, check: F, ) -> Option<(&mut T, ResourceId)> where F: Fn(&R, &Option) -> bool, - R: Resource

, - P: ResourcePath, + R: Resource, { profile_method!(recognize_mut_checked); diff --git a/src/service.rs b/src/service.rs index 975556197..f15cbfc9f 100644 --- a/src/service.rs +++ b/src/service.rs @@ -307,9 +307,11 @@ impl ServiceRequest { } } -impl Resource for ServiceRequest { +impl Resource for ServiceRequest { + type Path = Url; + #[inline] - fn resource_path(&mut self) -> &mut Path { + fn resource_path(&mut self) -> &mut Path { self.match_info_mut() } }