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
51
52
53
54
55
56
use crate::errors::SendError;
use futures::{channel::mpsc, SinkExt};

/// Internal wrapper over sent messages with additional types
/// of requests.
#[derive(Debug)]
pub(crate) enum Message<Input> {
    /// User-defined message.
    Message(Input),
    /// Request to stop the Mailbox.
    StopRequest,
}

impl<Input> From<Input> for Message<Input> {
    fn from(input: Input) -> Self {
        Self::Message(input)
    }
}

/// Address is an entity capable of sending messages.
/// It represents a sender side of communication, and the receiver side is represented using [Mailbox](../mailbox/struct.Mailbox.html).
#[derive(Debug)]
pub struct Address<Input> {
    sender: mpsc::Sender<Message<Input>>,
}

impl<Input> Clone for Address<Input> {
    fn clone(&self) -> Self {
        Self {
            sender: self.sender.clone(),
        }
    }
}

impl<Input> Address<Input> {
    /// Internal constructor for the `Address` object.
    pub(crate) fn new(sender: mpsc::Sender<Message<Input>>) -> Self {
        Self { sender }
    }

    /// Sends a message to the corresponding `Mailbox`.
    pub async fn send(&mut self, message: Input) -> Result<(), SendError> {
        self.sender
            .send(message.into())
            .await
            .map_err(|_| SendError::ReceiverDisconnected)
    }

    /// Sends a stop request to the corresponding `Mailbox`.
    pub async fn stop(&mut self) -> Result<(), SendError> {
        self.sender
            .send(Message::StopRequest)
            .await
            .map_err(|_| SendError::ReceiverDisconnected)
    }
}