simplify Resource trait

This commit is contained in:
Ali MJ Al-Nasrawy 2022-01-04 18:38:50 +03:00
parent 85c9b1a263
commit c2f736fa00
5 changed files with 36 additions and 23 deletions

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,22 @@ 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
}
}

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,10 @@ 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 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()
} }
} }