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
use crate::Message;
use anyhow::Result;
use std::future::Future;
use std::pin::Pin;

pub(crate) type CallerFn<T> = Box<
    dyn Fn(T) -> Pin<Box<dyn Future<Output = Result<<T as Message>::Result>> + Send + 'static>>
        + 'static,
>;

pub(crate) type SenderFn<T> = Box<dyn Fn(T) -> Result<()> + 'static>;

/// Caller of a specific message type
pub struct Caller<T: Message>(pub(crate) CallerFn<T>);

impl<T: Message> Caller<T> {
    pub async fn call(&mut self, msg: T) -> Result<T::Result> {
        self.0(msg).await
    }
}

/// Sender of a specific message type
pub struct Sender<T: Message>(pub(crate) SenderFn<T>);

impl<T: Message<Result = ()>> Sender<T> {
    pub fn send(&mut self, msg: T) -> Result<()> {
        self.0(msg)
    }
}