Skip to content

Commit

Permalink
simplify Resource trait (#2568)
Browse files Browse the repository at this point in the history
Co-authored-by: Rob Ede <robjtede@icloud.com>
  • Loading branch information
aliemjay and robjtede committed Jan 5, 2022
1 parent 0f7292c commit 49cfabe
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 23 deletions.
4 changes: 4 additions & 0 deletions 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<Path<_>>`. [#2568]

[#2568]: https://github.com/actix/actix-web/pull/2568


## 0.5.0-beta.4 - 2022-01-04
Expand Down
36 changes: 33 additions & 3 deletions 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;
Expand Down Expand Up @@ -213,8 +213,38 @@ 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
}
}

#[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();
}
}
5 changes: 2 additions & 3 deletions actix-router/src/resource.rs
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions actix-router/src/resource_path.rs
Expand Up @@ -2,8 +2,11 @@ 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 of resource's path returned in `resource_path`.
type Path: ResourcePath;

fn resource_path(&mut self) -> &mut Path<Self::Path>;
}

pub trait ResourcePath {
Expand Down
22 changes: 9 additions & 13 deletions 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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions src/service.rs
Expand Up @@ -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()
}
}
Expand Down

0 comments on commit 49cfabe

Please sign in to comment.