mm1_core/context/
ask.rs

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::future::Future;

use mm1_address::address::Address;
use mm1_common::errors::error_of::ErrorOf;
use mm1_common::impl_error_kind;

use crate::context::{Fork, ForkErrorKind, Recv, RecvErrorKind, Tell, TellErrorKind};
use crate::envelope::Envelope;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AskErrorKind {
    Fork(ForkErrorKind),
    Tell(TellErrorKind),
    Recv(RecvErrorKind),
}

pub trait Ask: Tell + Fork + Recv {
    fn ask<Req>(
        &mut self,
        to: Address,
        make_request: impl FnOnce(Address) -> Req + Send,
    ) -> impl Future<Output = Result<Envelope, ErrorOf<AskErrorKind>>> + Send
    where
        Req: Send + 'static,
    {
        async move {
            let mut forked = self
                .fork()
                .await
                .map_err(|e| e.map_kind(AskErrorKind::Fork))?;

            let reply_to = forked.address();
            let request = make_request(reply_to);
            self.tell(to, request)
                .await
                .map_err(|e| e.map_kind(AskErrorKind::Tell))?;

            let inbound = forked
                .recv()
                .await
                .map_err(|e| e.map_kind(AskErrorKind::Recv))?;

            Ok(inbound)
        }
    }
}

impl_error_kind!(AskErrorKind);

impl<T> Ask for T where T: Tell + Fork + Recv {}