mediator/
request.rs

1/// Represents a request to the mediator.
2pub trait Request<Res> {}
3
4/// Handles a request from the mediator.
5pub trait RequestHandler<Req, Res>
6where
7    Req: Request<Res>,
8{
9    /// Handle a request and returns the response.
10    fn handle(&mut self, req: Req) -> Res;
11}
12
13/// Handles an async request from the mediator.
14#[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    /// Handle a request and returns the response.
21    async fn handle(&mut self, req: Req) -> Res;
22}
23
24///////////////////// Implementations /////////////////////
25
26impl<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}