mm1_core/context/
messaging.rs

1use mm1_address::address::Address;
2use mm1_common::errors::error_of::ErrorOf;
3use mm1_common::impl_error_kind;
4use mm1_proto::{Message, message};
5
6use crate::envelope::{Envelope, EnvelopeHeader};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[message(base_path = ::mm1_proto)]
10pub enum RecvErrorKind {
11    Closed,
12}
13
14impl_error_kind!(RecvErrorKind);
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17#[message(base_path = ::mm1_proto)]
18pub enum SendErrorKind {
19    InternalError,
20    NotFound,
21    Closed,
22    Full,
23}
24
25pub trait Messaging {
26    fn address(&self) -> Address;
27
28    fn recv(&mut self) -> impl Future<Output = Result<Envelope, ErrorOf<RecvErrorKind>>> + Send;
29
30    fn close(&mut self) -> impl Future<Output = ()> + Send;
31
32    fn send(
33        &mut self,
34        envelope: Envelope,
35    ) -> impl Future<Output = Result<(), ErrorOf<SendErrorKind>>> + Send;
36}
37
38impl_error_kind!(SendErrorKind);
39
40pub trait Tell: Messaging {
41    fn tell<M>(
42        &mut self,
43        to: Address,
44        message: M,
45    ) -> impl Future<Output = Result<(), ErrorOf<SendErrorKind>>> + Send
46    where
47        M: Message,
48    {
49        let info = EnvelopeHeader::to_address(to);
50        let envelope = Envelope::new(info, message);
51        self.send(envelope.into_erased())
52    }
53}
54
55impl<T> Tell for T where T: Messaging {}