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::ops::Index;
use std::ops::{DerefMut, Index};
use firestorm::profile_method;
use serde::de;
@ -213,8 +213,22 @@ impl<T: ResourcePath> Index<usize> for Path<T> {
}
}
impl<T: ResourcePath> Resource<T> for Path<T> {
fn resource_path(&mut self) -> &mut Self {
impl<T: ResourcePath> Resource for Path<T> {
type Path = T;
fn resource_path(&mut self) -> &mut Path<Self::Path> {
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_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,
resource: &mut R,
check_fn: F,
user_data: U,
) -> bool
where
R: Resource<T>,
T: ResourcePath,
R: Resource,
F: FnOnce(&R, U) -> bool,
{
profile_method!(capture_match_info_fn);

View File

@ -2,8 +2,10 @@ use crate::Path;
// TODO: this trait is necessary, document it
// see impl Resource for ServiceRequest
pub trait Resource<T: ResourcePath> {
fn resource_path(&mut self) -> &mut Path<T>;
pub trait Resource {
type Path: ResourcePath;
fn resource_path(&mut self) -> &mut Path<Self::Path>;
}
pub trait ResourcePath {

View File

@ -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<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
R: Resource<P>,
P: ResourcePath,
R: Resource,
{
profile_method!(recognize);
@ -42,10 +41,9 @@ impl<T, U> Router<T, U> {
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
R: Resource<P>,
P: ResourcePath,
R: Resource,
{
profile_method!(recognize_mut);
@ -58,11 +56,10 @@ impl<T, U> Router<T, U> {
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
F: Fn(&R, &Option<U>) -> bool,
R: Resource<P>,
P: ResourcePath,
R: Resource,
{
profile_method!(recognize_checked);
@ -75,15 +72,14 @@ impl<T, U> Router<T, U> {
None
}
pub fn recognize_mut_fn<R, P, F>(
pub fn recognize_mut_fn<R, F>(
&mut self,
resource: &mut R,
check: F,
) -> Option<(&mut T, ResourceId)>
where
F: Fn(&R, &Option<U>) -> bool,
R: Resource<P>,
P: ResourcePath,
R: Resource,
{
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]
fn resource_path(&mut self) -> &mut Path<Url> {
fn resource_path(&mut self) -> &mut Path<Self::Path> {
self.match_info_mut()
}
}