1pub trait Request<Res> {}
3
4pub trait RequestHandler<Req, Res>
6where
7 Req: Request<Res>,
8{
9 fn handle(&mut self, req: Req) -> Res;
11}
12
13#[cfg(feature = "async")]
15#[cfg_attr(feature = "async", async_trait::async_trait)]
16pub trait AsyncRequestHandler<Req, Res>
17where
18 Req: Request<Res> + Send,
19{
20 async fn handle(&mut self, req: Req) -> Res;
22}
23
24impl<Req, Res, F> RequestHandler<Req, Res> for F
27where
28 F: FnMut(Req) -> Res,
29 Req: Request<Res>,
30{
31 fn handle(&mut self, req: Req) -> Res {
32 self(req)
33 }
34}
35
36#[cfg(feature = "async")]
37#[cfg_attr(feature = "async", async_trait::async_trait)]
38impl<Req, Res, F, U> AsyncRequestHandler<Req, Res> for F
39where
40 Req: Request<Res> + Send + 'static,
41 F: FnMut(Req) -> U + Sync + Send,
42 U: std::future::Future<Output = Res> + Send,
43{
44 async fn handle(&mut self, req: Req) -> Res {
45 self(req).await
46 }
47}