mm1_core/context/
messaging.rs

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