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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use crate::*;
use event_listener as el;
use futures::{Future, FutureExt, Stream};
use std::{
    fmt::Debug,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

pub struct Inbox<T> {
    // The underlying channel
    channel: Arc<Channel<T>>,
    // The listener for receiving events
    listener: Option<el::EventListener>,
    // Whether this inbox has signaled halt yet
    signaled_halt: bool,
}

impl<T> Inbox<T> {
    pub(crate) fn from_channel(channel: Arc<Channel<T>>) -> Self {
        Inbox {
            channel,
            listener: None,
            signaled_halt: false,
        }
    }

    /// Create a new inbox from a channel. Returns `None` if the `Channel` has exited.
    pub(crate) fn try_from_channel(channel: Arc<Channel<T>>) -> Option<Self> {
        match channel.try_add_inbox() {
            Ok(()) => Some(Self {
                channel,
                listener: None,
                signaled_halt: false,
            }),
            Err(()) => None,
        }
    }

    /// This will attempt to receive a message from the inbox.
    pub fn try_recv(&mut self) -> Result<Option<T>, RecvError> {
        // If we have not yet signaled for supervision yet, and there is a signal ready.
        if !self.signaled_halt && self.channel.inbox_should_halt() {
            self.signaled_halt = true;
            Err(RecvError::Halted)

        // Otherwise, we can just try to receive a message.
        } else {
            self.channel
                .take_next_msg()
                .map_err(|()| RecvError::ClosedAndEmpty)
        }
    }

    /// This will wait for a message in the inbox.
    pub fn recv(&mut self) -> Rcv<'_, T> {
        Rcv { inbox: self }
    }

    /// See [Address::try_send]
    pub fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
        try_send(&self.channel, msg)
    }

    /// See [Address::send_now]
    pub fn send_now(&self, msg: T) -> Result<(), TrySendError<T>> {
        send_now(&self.channel, msg)
    }

    /// See [Address::send]
    pub fn send(&self, msg: T) -> Snd<'_, T> {
        send(&self.channel, msg)
    }

    /// See [Address::send_blocking]
    pub fn send_blocking(&self, msg: T) -> Result<(), SendError<T>> {
        send_blocking(&self.channel, msg)
    }

    /// Receive a message while blocking the scheduler.
    pub fn recv_blocking(&mut self) -> Result<T, RecvError> {
        loop {
            match self.try_recv() {
                Ok(None) => (),
                Ok(Some(msg)) => {
                    self.listener = None;
                    return Ok(msg);
                }
                Err(signal) => {
                    self.listener = None;
                    match signal {
                        RecvError::Halted => return Err(RecvError::Halted),
                        RecvError::ClosedAndEmpty => return Err(RecvError::ClosedAndEmpty),
                    }
                }
            }

            self.channel.recv_listener().wait();
        }
    }

    /// Close the channel.
    pub fn close(&self) -> bool {
        self.channel.close()
    }

    /// Halt all inboxes linked to the channel.
    pub fn halt(&self) {
        self.channel.halt_n(u32::MAX)
    }

    /// Halt n inboxes linked to the channel.
    pub fn halt_some(&self, n: u32) {
        self.channel.halt_n(n)
    }

    /// Get the amount of inboxes linked to the channel.
    pub fn inbox_count(&self) -> usize {
        self.channel.inbox_count()
    }

    /// Get the amount of messages linked to the channel.
    pub fn message_count(&self) -> usize {
        self.channel.msg_count()
    }

    /// Get the amount of addresses linked to the channel.
    pub fn address_count(&self) -> usize {
        self.channel.address_count()
    }

    /// Whether the channel has been closed.
    pub fn is_closed(&self) -> bool {
        self.channel.is_closed()
    }

    /// Get the capacity of the channel.
    pub fn capacity(&self) -> &Capacity {
        self.channel.capacity()
    }

    /// Clone, for use within the crate.
    pub(crate) fn clone_inbox(&self) -> Self {
        self.channel.add_inbox();
        Self {
            channel: self.channel.clone(),
            listener: None,
            signaled_halt: self.signaled_halt.clone(),
        }
    }
}

impl<T> Stream for Inbox<T> {
    type Item = Result<T, Halted>;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        loop {
            match self.try_recv() {
                Ok(None) => (),
                Ok(Some(msg)) => {
                    self.listener = None;
                    return Poll::Ready(Some(Ok(msg)));
                }
                Err(signal) => {
                    self.listener = None;
                    match signal {
                        RecvError::Halted => return Poll::Ready(Some(Err(Halted))),
                        RecvError::ClosedAndEmpty => return Poll::Ready(None),
                    }
                }
            }

            if self.listener.is_none() {
                self.listener = Some(self.channel.recv_listener())
            }

            match self.listener.as_mut().unwrap().poll_unpin(cx) {
                Poll::Ready(()) => {}
                Poll::Pending => return Poll::Pending,
            }
        }
    }
}

impl<T> Drop for Inbox<T> {
    fn drop(&mut self) {
        self.channel.remove_inbox()
    }
}

impl<T> Debug for Inbox<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Inbox")
            .field("listener", &self.listener)
            .field("signaled_halt", &self.signaled_halt)
            .finish()
    }
}

//------------------------------------------------------------------------------------------------
//  Rcv
//------------------------------------------------------------------------------------------------

pub struct Rcv<'a, T> {
    inbox: &'a mut Inbox<T>,
}

impl<'a, T> Unpin for Rcv<'a, T> {}

impl<'a, T> Future for Rcv<'a, T> {
    type Output = Result<T, RecvError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        loop {
            match self.inbox.try_recv() {
                Ok(None) => (),
                Ok(Some(msg)) => {
                    return Poll::Ready(Ok(msg));
                }
                Err(signal) => match signal {
                    RecvError::Halted => return Poll::Ready(Err(RecvError::Halted)),
                    RecvError::ClosedAndEmpty => {
                        return Poll::Ready(Err(RecvError::ClosedAndEmpty))
                    }
                },
            }

            if self.inbox.listener.is_none() {
                self.inbox.listener = Some(self.inbox.channel.recv_listener())
            }

            match self.inbox.listener.as_mut().unwrap().poll_unpin(cx) {
                Poll::Ready(()) => self.inbox.listener = None,
                Poll::Pending => return Poll::Pending,
            }
        }
    }
}

impl<'a, T> Debug for Rcv<'a, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Rcv").field("inbox", &self.inbox).finish()
    }
}

//------------------------------------------------------------------------------------------------
//  Errors
//------------------------------------------------------------------------------------------------

/// This Inbox has been halted.
#[derive(Debug, thiserror::Error)]
#[error("This inbox has been halted")]
pub struct Halted;

/// Error returned when receiving a message from an inbox.
/// Reasons can be:
/// * `Halted`: This Inbox has been halted and should now exit.
/// * `ClosedAndEmpty`: This Inbox is closed and empty, it can no longer receive new messages.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RecvError {
    /// This inbox has been halted and should now exit.
    Halted,
    /// This inbox has been closed, and contains no more messages. It was closed either because
    /// all addresses have been dropped, or because it was manually closed.
    ClosedAndEmpty,
}