-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat-328/add ExtensionMatcher struct #346
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::{ | ||
matcher::{Extensions, Matcher}, | ||
Context, | ||
}; | ||
use std::any::Any; | ||
|
||
pub struct ExtensionMatcher<T: Send + Sync> { | ||
marker: T, | ||
} | ||
|
||
struct Const<T: Send + Sync>(T); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This I would replace with something like the following: mod private {
use std::marker::PhantomData;
pub(super) trait ExtensionPredicate<T>: Send + Sync + 'static {
fn call(&self, value: &T) -> bool;
}
pub(super) struct PredicateConst<T>(pub(super) T);
impl<T> ExtensionPredicate<T> for PredicateConst<T>
where
T: Clone + Eq + Send + Sync + 'static,
{
#[inline]
fn call(&self, value: &T) -> bool {
self.0.eq(value)
}
}
pub(super) struct PredicateFn<F, T>(pub(super) F, pub(super) PhantomData<fn() -> T>);
impl<F, T> ExtensionPredicate<T> for PredicateFn<F, T>
where
F: Fn(&T) -> bool + Send + Sync + 'static,
T: Clone + Eq + Send + Sync + 'static,
{
#[inline]
fn call(&self, value: &T) -> bool {
self.0(value)
}
}
} |
||
struct FnBox(Box<dyn Fn(&dyn Any) -> bool + Send + Sync>); | ||
|
||
impl ExtensionMatcher<FnBox> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is how you can create the fn variant: impl<F, T> ExtensionMatcher<private::PredicateFn<F, T>, T>
where
F: Fn(&T) -> bool + Send + Sync + 'static,
T: Clone + Eq + Send + Sync + 'static,
{
/// Create a new [`ExtensionMatcher`] with a predicate `F`,
/// that will be called using the found `T` (if one is present),
/// to let it decide whether or not the extension value is a match or not.
pub fn with_fn(predicate: F) -> Self {
Self {
predicate: private::PredicateFn(predicate, PhantomData),
_marker: PhantomData,
}
}
} |
||
pub fn with<T: Send + Sync + 'static, F: Fn(&T) -> bool + Send + Sync + 'static>(f: F) -> Self { | ||
let wrapped_fn = move |v: &dyn Any| { | ||
if let Some(concrete) = v.downcast_ref::<T>() { | ||
f(concrete) | ||
} else { | ||
false | ||
} | ||
}; | ||
Self { | ||
marker: FnBox(Box::new(wrapped_fn)), | ||
} | ||
} | ||
} | ||
|
||
impl<T: Send + Sync + Sync + std::cmp::PartialEq> ExtensionMatcher<Const<T>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be replaced with the following: impl<T> ExtensionMatcher<private::PredicateConst<T>, T>
where
T: Clone + Eq + Send + Sync + 'static,
{
/// Create a new [`ExtensionMatcher`] with a const value `T`,
/// that will be [`Eq`]-checked to find a match.
pub fn with_const(value: T) -> Self {
Self {
predicate: private::PredicateConst(value),
_marker: PhantomData,
}
}
} |
||
pub fn constant(value: T) -> Self { | ||
Self { | ||
marker: Const(value), | ||
} | ||
} | ||
} | ||
|
||
impl<T: Send + Sync + std::cmp::PartialEq + 'static, State, Request> Matcher<State, Request> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks to the private trait trick you can now just implement the impl<State, Request, P, T> Matcher<State, Request> for ExtensionMatcher<P, T>
where
State: Clone + Send + Sync + 'static,
Request: Send + 'static,
T: Clone + Send + Sync + 'static,
P: private::ExtensionPredicate<T>,
{
fn matches(&self, _ext: Option<&mut Extensions>, ctx: &Context<State>, _req: &Request) -> bool {
ctx.get::<T>()
.map(|v| self.predicate.call(v))
.unwrap_or_default()
}
} |
||
for ExtensionMatcher<Const<T>> | ||
{ | ||
fn matches(&self, _ext: Option<&mut Extensions>, ctx: &Context<State>, _req: &Request) -> bool { | ||
ctx.get::<T>() | ||
.map(|v| v == &self.marker.0) | ||
.unwrap_or_default() | ||
} | ||
} | ||
|
||
impl<State, Request> Matcher<State, Request> for ExtensionMatcher<FnBox> { | ||
fn matches(&self, _ext: Option<&mut Extensions>, ctx: &Context<State>, _req: &Request) -> bool { | ||
ctx.get::<FnBox>() | ||
.map(|v| (self.marker.0)(v)) | ||
.unwrap_or_default() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
GlenDC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use super::*; | ||
use crate::context::Extensions; | ||
|
||
#[derive(Clone, PartialEq)] | ||
struct MyMarker(i32); | ||
|
||
#[test] | ||
fn test_extension_matcher() { | ||
let mut ext = Extensions::new(); | ||
ext.insert(MyMarker(10)); | ||
let matcher = ExtensionMatcher::constant(MyMarker(10)); | ||
assert!(matcher.matches(Some(&mut ext), &Context::default(), &10)); | ||
} | ||
|
||
#[test] | ||
fn test_fn_extension_matcher() { | ||
let mut ext = Extensions::new(); | ||
ext.insert(MyMarker(10)); | ||
let matcher = ExtensionMatcher::with(|v: &MyMarker| v.0.eq(&10)); | ||
assert!(matcher.matches(Some(&mut ext), &Context::default(), &10)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how I would define the struct: