1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::handler::Handler;
use crate::internal_prelude::*;

pub trait Middleware: Send + Sync {
    fn handle<'t, 'n, 'a>(
        &'t self,
        req: Request,
        next: &'n dyn Handler,
    ) -> BoxFuture<'a, Result<Response>>
    where
        't: 'a,
        'n: 'a,
        Self: 'a;

    fn boxed(self) -> Box<dyn Middleware>
    where
        Self: Sized + 'static,
    {
        Box::new(self)
    }
}

impl Middleware for Box<dyn Middleware> {
    fn handle<'t, 'n, 'a>(
        &'t self,
        req: Request,
        next: &'n dyn Handler,
    ) -> BoxFuture<'a, Result<Response>>
    where
        't: 'a,
        'n: 'a,
        Self: 'a,
    {
        Middleware::handle(&**self, req, next)
    }
}