workflow_nw/ipc/
method.rs1use crate::ipc::imports::*;
3
4#[async_trait]
7pub(crate) trait MethodTrait: Send + Sync + 'static {
8 async fn call_with_borsh(&self, data: &[u8]) -> ResponseResult<Vec<u8>>;
9}
10
11pub type MethodFn<Req, Resp> =
13 Arc<Box<dyn Send + Sync + Fn(Req) -> MethodFnReturn<Resp> + 'static>>;
14
15pub type MethodFnReturn<T> = Pin<Box<dyn Send + 'static + Future<Output = ResponseResult<T>>>>;
17
18pub struct Method<Req, Resp>
20where
21 Req: MsgT,
22 Resp: MsgT,
23{
24 method: MethodFn<Req, Resp>,
25}
26
27impl<Req, Resp> Method<Req, Resp>
28where
29 Req: MsgT,
30 Resp: MsgT,
31{
32 pub fn new<FN>(method_fn: FN) -> Method<Req, Resp>
35 where
36 FN: Send + Sync + Fn(Req) -> MethodFnReturn<Resp> + 'static,
37 {
38 Method {
39 method: Arc::new(Box::new(method_fn)),
40 }
41 }
42}
43
44#[async_trait]
45impl<Req, Resp> MethodTrait for Method<Req, Resp>
46where
47 Req: MsgT,
48 Resp: MsgT,
49{
50 async fn call_with_borsh(&self, data: &[u8]) -> ResponseResult<Vec<u8>> {
51 let req = Req::try_from_slice(data)?;
52 let resp = (self.method)(req).await;
53 Ok(borsh::to_vec(&resp)?)
54 }
55}