|
| 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 | +} |
0 commit comments