Skip to content

Commit

Permalink
move child trait method into the seal where it is truly private
Browse files Browse the repository at this point in the history
Close #333
  • Loading branch information
GlenDC committed Oct 13, 2024
1 parent 994d4af commit 1b6db41
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 351 deletions.
142 changes: 73 additions & 69 deletions rama-core/src/matcher/mfn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,86 +14,45 @@ pub fn match_fn<F, A>(f: F) -> MatchFnBox<F, A> {
///
/// You do not need to implement this trait yourself.
/// Instead, you need to use the [`match_fn`] function to create a [`MatchFn`].
pub trait MatchFn<S, Request, A>: private::Sealed<S, Request, A> + Send + Sync + 'static {
/// returns true on a match, false otherwise
///
/// `ext` is None in case the callee is not interested in collecting potential
/// match metadata gathered during the matching process. An example of this
/// path parameters for an http Uri matcher.
fn call(&self, ext: Option<&mut Extensions>, ctx: &Context<S>, req: &Request) -> bool;
}
pub trait MatchFn<S, Request, A>: private::Sealed<S, Request, A> + Send + Sync + 'static {}

impl<F, S, Request> MatchFn<S, Request, ()> for F
where
F: Fn() -> bool + Send + Sync + 'static,
{
fn call(&self, _ext: Option<&mut Extensions>, _ctx: &Context<S>, _req: &Request) -> bool {
(self)()
}
}
impl<F, S, Request> MatchFn<S, Request, ()> for F where F: Fn() -> bool + Send + Sync + 'static {}

impl<F, S, Request> MatchFn<S, Request, (Request,)> for F
where
F: Fn(&Request) -> bool + Send + Sync + 'static,
impl<F, S, Request> MatchFn<S, Request, (Request,)> for F where
F: Fn(&Request) -> bool + Send + Sync + 'static
{
fn call(&self, _ext: Option<&mut Extensions>, _ctx: &Context<S>, req: &Request) -> bool {
(self)(req)
}
}

impl<F, S, Request> MatchFn<S, Request, (Context<S>, Request)> for F
where
F: Fn(&Context<S>, &Request) -> bool + Send + Sync + 'static,
impl<F, S, Request> MatchFn<S, Request, (Context<S>, Request)> for F where
F: Fn(&Context<S>, &Request) -> bool + Send + Sync + 'static
{
fn call(&self, _ext: Option<&mut Extensions>, ctx: &Context<S>, req: &Request) -> bool {
(self)(ctx, req)
}
}

impl<F, S, Request> MatchFn<S, Request, (Option<&mut Extensions>, Context<S>, Request)> for F
where
F: Fn(Option<&mut Extensions>, &Context<S>, &Request) -> bool + Send + Sync + 'static,
impl<F, S, Request> MatchFn<S, Request, (Option<&mut Extensions>, Context<S>, Request)> for F where
F: Fn(Option<&mut Extensions>, &Context<S>, &Request) -> bool + Send + Sync + 'static
{
fn call(&self, ext: Option<&mut Extensions>, ctx: &Context<S>, req: &Request) -> bool {
(self)(ext, ctx, req)
}
}

impl<F, S, Request> MatchFn<S, Request, ((), (), Option<&mut Extensions>, Request)> for F
where
F: Fn(Option<&mut Extensions>, &Request) -> bool + Send + Sync + 'static,
impl<F, S, Request> MatchFn<S, Request, ((), (), Option<&mut Extensions>, Request)> for F where
F: Fn(Option<&mut Extensions>, &Request) -> bool + Send + Sync + 'static
{
fn call(&self, ext: Option<&mut Extensions>, _ctx: &Context<S>, req: &Request) -> bool {
(self)(ext, req)
}
}

impl<F, S, Request> MatchFn<S, Request, ((), (), (), (), Option<&mut Extensions>)> for F
where
F: Fn(Option<&mut Extensions>) -> bool + Send + Sync + 'static,
impl<F, S, Request> MatchFn<S, Request, ((), (), (), (), Option<&mut Extensions>)> for F where
F: Fn(Option<&mut Extensions>) -> bool + Send + Sync + 'static
{
fn call(&self, ext: Option<&mut Extensions>, _ctx: &Context<S>, _req: &Request) -> bool {
(self)(ext)
}
}

impl<F, S, Request> MatchFn<S, Request, ((), (), (), (), (), Context<S>)> for F
where
F: Fn(&Context<S>) -> bool + Send + Sync + 'static,
impl<F, S, Request> MatchFn<S, Request, ((), (), (), (), (), Context<S>)> for F where
F: Fn(&Context<S>) -> bool + Send + Sync + 'static
{
fn call(&self, _ext: Option<&mut Extensions>, ctx: &Context<S>, _req: &Request) -> bool {
(self)(ctx)
}
}

impl<F, S, Request>
MatchFn<S, Request, ((), (), (), (), (), (), Option<&mut Extensions>, Context<S>)> for F
where
F: Fn(Option<&mut Extensions>, &Context<S>) -> bool + Send + Sync + 'static,
{
fn call(&self, ext: Option<&mut Extensions>, ctx: &Context<S>, _req: &Request) -> bool {
(self)(ext, ctx)
}
}

/// The public wrapper type for [`MatchFn`].
Expand Down Expand Up @@ -133,40 +92,85 @@ where
mod private {
use super::*;

pub trait Sealed<S, Request, A> {}
pub trait Sealed<S, Request, A> {
/// returns true on a match, false otherwise
///
/// `ext` is None in case the callee is not interested in collecting potential
/// match metadata gathered during the matching process. An example of this
/// path parameters for an http Uri matcher.
fn call(&self, ext: Option<&mut Extensions>, ctx: &Context<S>, req: &Request) -> bool;
}

impl<F, S, Request> Sealed<S, Request, ()> for F
where
F: Fn() -> bool + Send + Sync + 'static,
{
fn call(&self, _ext: Option<&mut Extensions>, _ctx: &Context<S>, _req: &Request) -> bool {
(self)()
}
}

impl<F, S, Request> Sealed<S, Request, ()> for F where F: Fn() -> bool + Send + Sync + 'static {}
impl<F, S, Request> Sealed<S, Request, (Request,)> for F where
F: Fn(&Request) -> bool + Send + Sync + 'static
impl<F, S, Request> Sealed<S, Request, (Request,)> for F
where
F: Fn(&Request) -> bool + Send + Sync + 'static,
{
fn call(&self, _ext: Option<&mut Extensions>, _ctx: &Context<S>, req: &Request) -> bool {
(self)(req)
}
}
impl<F, S, Request> Sealed<S, Request, (Context<S>, Request)> for F where
F: Fn(&Context<S>, &Request) -> bool + Send + Sync + 'static

impl<F, S, Request> Sealed<S, Request, (Context<S>, Request)> for F
where
F: Fn(&Context<S>, &Request) -> bool + Send + Sync + 'static,
{
fn call(&self, _ext: Option<&mut Extensions>, ctx: &Context<S>, req: &Request) -> bool {
(self)(ctx, req)
}
}
impl<F, S, Request> Sealed<S, Request, (Option<&mut Extensions>, Context<S>, Request)> for F where
F: Fn(Option<&mut Extensions>, &Context<S>, &Request) -> bool + Send + Sync + 'static

impl<F, S, Request> Sealed<S, Request, (Option<&mut Extensions>, Context<S>, Request)> for F
where
F: Fn(Option<&mut Extensions>, &Context<S>, &Request) -> bool + Send + Sync + 'static,
{
fn call(&self, ext: Option<&mut Extensions>, ctx: &Context<S>, req: &Request) -> bool {
(self)(ext, ctx, req)
}
}
impl<F, S, Request> Sealed<S, Request, ((), (), Option<&mut Extensions>, Request)> for F where
F: Fn(Option<&mut Extensions>, &Request) -> bool + Send + Sync + 'static

impl<F, S, Request> Sealed<S, Request, ((), (), Option<&mut Extensions>, Request)> for F
where
F: Fn(Option<&mut Extensions>, &Request) -> bool + Send + Sync + 'static,
{
fn call(&self, ext: Option<&mut Extensions>, _ctx: &Context<S>, req: &Request) -> bool {
(self)(ext, req)
}
}

impl<F, S, Request> Sealed<S, Request, ((), (), (), (), Option<&mut Extensions>)> for F where
F: Fn(Option<&mut Extensions>) -> bool + Send + Sync + 'static
impl<F, S, Request> Sealed<S, Request, ((), (), (), (), Option<&mut Extensions>)> for F
where
F: Fn(Option<&mut Extensions>) -> bool + Send + Sync + 'static,
{
fn call(&self, ext: Option<&mut Extensions>, _ctx: &Context<S>, _req: &Request) -> bool {
(self)(ext)
}
}

impl<F, S, Request> Sealed<S, Request, ((), (), (), (), (), Context<S>)> for F where
F: Fn(&Context<S>) -> bool + Send + Sync + 'static
impl<F, S, Request> Sealed<S, Request, ((), (), (), (), (), Context<S>)> for F
where
F: Fn(&Context<S>) -> bool + Send + Sync + 'static,
{
fn call(&self, _ext: Option<&mut Extensions>, ctx: &Context<S>, _req: &Request) -> bool {
(self)(ctx)
}
}

impl<F, S, Request>
Sealed<S, Request, ((), (), (), (), (), (), Option<&mut Extensions>, Context<S>)> for F
where
F: Fn(Option<&mut Extensions>, &Context<S>) -> bool + Send + Sync + 'static,
{
fn call(&self, ext: Option<&mut Extensions>, ctx: &Context<S>, _req: &Request) -> bool {
(self)(ext, ctx)
}
}
}
Loading

0 comments on commit 1b6db41

Please sign in to comment.