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
37
38
39
40
41
42
43
44
45
46
use crate::{
    request::Request,
    responder::{DynResponder, Responder},
};
use futures::{Future, FutureExt};
use std::pin::Pin;

pub trait Handler {
    type Responder: Responder;

    type Future: Future<Output = Self::Responder>;

    fn handle(&self, req: Request) -> Self::Future;
}

impl<R, Fut, F> Handler for F
where
    R: Responder,
    Fut: Future<Output = R> + Send + 'static,
    F: Fn(Request) -> Fut,
{
    type Future = Box<dyn Future<Output = Self::Responder> + Unpin + Send>;
    type Responder = R;

    #[inline]
    fn handle(&self, req: Request) -> Self::Future {
        Box::new(Box::pin((*self)(req)))
    }
}

#[doc(hidden)]
pub trait DynHandler {
    fn handle(&self, req: Request) -> Pin<Box<dyn Future<Output = Box<dyn DynResponder + Send>> + Unpin + Send>>;
}

impl<R, Fut, F> DynHandler for F
where
    R: 'static + Responder + Send,
    Fut: 'static + Future<Output = R> + Send + Unpin,
    F: Handler<Future = Fut, Responder = R>,
{
    #[inline]
    fn handle(&self, req: Request) -> Pin<Box<dyn Future<Output = Box<dyn DynResponder + Send>> + Unpin + Send>> {
        Box::pin(self.handle(req).map(|r| Box::new(Some(r)) as Box<dyn DynResponder + Send>))
    }
}