1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00

simplify Resource trait (#2568)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Ali MJ Al-Nasrawy 2022-01-05 07:34:13 +03:00 committed by GitHub
parent 0f7292c69a
commit 49cfabeaf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 23 deletions

View file

@ -1,6 +1,10 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## 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<Path<_>>`. [#2568]
[#2568]: https://github.com/actix/actix-web/pull/2568
## 0.5.0-beta.4 - 2022-01-04 ## 0.5.0-beta.4 - 2022-01-04

View file

@ -1,5 +1,5 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::ops::Index; use std::ops::{DerefMut, Index};
use firestorm::profile_method; use firestorm::profile_method;
use serde::de; use serde::de;
@ -213,8 +213,38 @@ impl<T: ResourcePath> Index<usize> for Path<T> {
} }
} }
impl<T: ResourcePath> Resource<T> for Path<T> { impl<T: ResourcePath> Resource for Path<T> {
fn resource_path(&mut self) -> &mut Self { type Path = T;
fn resource_path(&mut self) -> &mut Path<Self::Path> {
self self
} }
} }
impl<T, P> Resource for T
where
T: DerefMut<Target = Path<P>>,
P: ResourcePath,
{
type Path = P;
fn resource_path(&mut self) -> &mut Path<Self::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();
}
}

View file

@ -678,15 +678,14 @@ impl ResourceDef {
/// assert!(!try_match(&resource, &mut path)); /// assert!(!try_match(&resource, &mut path));
/// assert_eq!(path.unprocessed(), "/user/admin/stars"); /// assert_eq!(path.unprocessed(), "/user/admin/stars");
/// ``` /// ```
pub fn capture_match_info_fn<R, T, F, U>( pub fn capture_match_info_fn<R, F, U>(
&self, &self,
resource: &mut R, resource: &mut R,
check_fn: F, check_fn: F,
user_data: U, user_data: U,
) -> bool ) -> bool
where where
R: Resource<T>, R: Resource,
T: ResourcePath,
F: FnOnce(&R, U) -> bool, F: FnOnce(&R, U) -> bool,
{ {
profile_method!(capture_match_info_fn); profile_method!(capture_match_info_fn);

View file

@ -2,8 +2,11 @@ use crate::Path;
// TODO: this trait is necessary, document it // TODO: this trait is necessary, document it
// see impl Resource for ServiceRequest // see impl Resource for ServiceRequest
pub trait Resource<T: ResourcePath> { pub trait Resource {
fn resource_path(&mut self) -> &mut Path<T>; /// Type of resource's path returned in `resource_path`.
type Path: ResourcePath;
fn resource_path(&mut self) -> &mut Path<Self::Path>;
} }
pub trait ResourcePath { pub trait ResourcePath {

View file

@ -1,6 +1,6 @@
use firestorm::profile_method; use firestorm::profile_method;
use crate::{IntoPatterns, Resource, ResourceDef, ResourcePath}; use crate::{IntoPatterns, Resource, ResourceDef};
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
pub struct ResourceId(pub u16); pub struct ResourceId(pub u16);
@ -26,10 +26,9 @@ impl<T, U> Router<T, U> {
} }
} }
pub fn recognize<R, P>(&self, resource: &mut R) -> Option<(&T, ResourceId)> pub fn recognize<R>(&self, resource: &mut R) -> Option<(&T, ResourceId)>
where where
R: Resource<P>, R: Resource,
P: ResourcePath,
{ {
profile_method!(recognize); profile_method!(recognize);
@ -42,10 +41,9 @@ impl<T, U> Router<T, U> {
None None
} }
pub fn recognize_mut<R, P>(&mut self, resource: &mut R) -> Option<(&mut T, ResourceId)> pub fn recognize_mut<R>(&mut self, resource: &mut R) -> Option<(&mut T, ResourceId)>
where where
R: Resource<P>, R: Resource,
P: ResourcePath,
{ {
profile_method!(recognize_mut); profile_method!(recognize_mut);
@ -58,11 +56,10 @@ impl<T, U> Router<T, U> {
None None
} }
pub fn recognize_fn<R, P, F>(&self, resource: &mut R, check: F) -> Option<(&T, ResourceId)> pub fn recognize_fn<R, F>(&self, resource: &mut R, check: F) -> Option<(&T, ResourceId)>
where where
F: Fn(&R, &Option<U>) -> bool, F: Fn(&R, &Option<U>) -> bool,
R: Resource<P>, R: Resource,
P: ResourcePath,
{ {
profile_method!(recognize_checked); profile_method!(recognize_checked);
@ -75,15 +72,14 @@ impl<T, U> Router<T, U> {
None None
} }
pub fn recognize_mut_fn<R, P, F>( pub fn recognize_mut_fn<R, F>(
&mut self, &mut self,
resource: &mut R, resource: &mut R,
check: F, check: F,
) -> Option<(&mut T, ResourceId)> ) -> Option<(&mut T, ResourceId)>
where where
F: Fn(&R, &Option<U>) -> bool, F: Fn(&R, &Option<U>) -> bool,
R: Resource<P>, R: Resource,
P: ResourcePath,
{ {
profile_method!(recognize_mut_checked); profile_method!(recognize_mut_checked);

View file

@ -307,9 +307,11 @@ impl ServiceRequest {
} }
} }
impl Resource<Url> for ServiceRequest { impl Resource for ServiceRequest {
type Path = Url;
#[inline] #[inline]
fn resource_path(&mut self) -> &mut Path<Url> { fn resource_path(&mut self) -> &mut Path<Self::Path> {
self.match_info_mut() self.match_info_mut()
} }
} }