Skip to content

Commit 713675b

Browse files
committed
Initial thoughts for unique-property
1 parent 370287c commit 713675b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

core/lib/src/router/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod router;
44
mod collider;
55
mod matcher;
6+
mod unique_property;
67

78
pub(crate) use router::*;
89
pub(crate) use collider::*;
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::any::Any;
2+
3+
use crate::{request::{FromRequest, Outcome}, Request};
4+
5+
6+
7+
pub trait UniqueProperty: Any {
8+
fn as_any(&self) -> &dyn Any;
9+
10+
fn matches(&self, other: &dyn Any) -> Option<bool>;
11+
12+
// TODO: matches request. We want this so we can make routing decisions later, although we could choose to rely
13+
// on the handler to do this for us.
14+
}
15+
16+
impl<T: Eq + Any> UniqueProperty for T {
17+
fn as_any(&self) -> &dyn Any {
18+
self
19+
}
20+
21+
fn matches(&self, other: &dyn Any) -> Option<bool> {
22+
other.downcast_ref().map(|other: &Self| other == self)
23+
}
24+
}
25+
26+
// pub struct UniqueInstance {
27+
// val: Arc<dyn Any>,
28+
// matcher: Box<dyn Fn(&dyn Any) -> Option<bool>>,
29+
// }
30+
31+
// impl UniqueInstance {
32+
// pub fn new<T: UniqueProperty + 'static>(val: T) -> Self {
33+
// let arc = Arc::new(val);
34+
// Self {
35+
// val: arc.clone(),
36+
// matcher: Box::new(move |v| v.downcast_ref().map(|v: &T| v == arc.as_ref())),
37+
// }
38+
// }
39+
40+
// /// Returns true iff `self` and `val` hold values of the same type, and have different
41+
// /// values.
42+
// pub fn prevents_collision(&self, val: &Self) -> bool {
43+
// (self.matcher)(val.val.as_ref()).map_or(false, |is_eq| !is_eq)
44+
// }
45+
// }
46+
47+
// async fn matches_request<'a, 'r, T: UniqueProperty + Eq + 'a>(val: &dyn UniqueProperty, req: &'r Request<'_>) -> bool where &'a T: FromRequest<'r> {
48+
// match <&T as FromRequest>::from_request(req).await {
49+
// Outcome::Success(v) => val.matches(&v).map_or(false, |v| v),
50+
// _ => false,
51+
// }
52+
// }
53+
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use super::*;
58+
use crate::http::uri::Host;
59+
use crate::uri;
60+
61+
#[test]
62+
fn basic_api() {
63+
// let instance = UniqueInstance::new(Host::new(uri!("my.example.com")));
64+
}
65+
}

tmp.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Pluggable routing
2+
3+
A 'unique property' is a type that implements `FromRequest` and `Eq`.
4+
5+
A route may have any number of 'unique properties', and routes A and B
6+
are considered colliding if they have colliding paths, and they do not
7+
share a 'unique property' type with different values.
8+
9+
E.g.: `Format(MediaType)`.
10+
11+

0 commit comments

Comments
 (0)