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
use std::fmt;
pub(crate) mod channel;
mod envelope;
mod message;
mod queue;
use actor::Actor;
use handler::{Handler, Message};
pub use self::envelope::{Envelope, EnvelopeProxy, ToEnvelope};
pub use self::message::{RecipientRequest, Request};
pub(crate) use self::channel::AddressReceiver;
use self::channel::{AddressSender, Sender};
pub enum SendError<T> {
Full(T),
Closed(T),
}
#[derive(Fail)]
pub enum MailboxError {
#[fail(display = "Mailbox has closed")]
Closed,
#[fail(display = "Message delivery timed out")]
Timeout,
}
impl<T> SendError<T> {
pub fn into_inner(self) -> T {
match self {
SendError::Full(msg) | SendError::Closed(msg) => msg,
}
}
}
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
SendError::Full(_) => write!(fmt, "SendError::Full(..)"),
SendError::Closed(_) => write!(fmt, "SendError::Closed(..)"),
}
}
}
impl<T> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
SendError::Full(_) => write!(fmt, "send failed because receiver is full"),
SendError::Closed(_) => write!(fmt, "send failed because receiver is gone"),
}
}
}
impl fmt::Debug for MailboxError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "MailboxError({})", self)
}
}
pub struct Addr<A: Actor> {
tx: AddressSender<A>,
}
impl<A: Actor> Addr<A> {
pub fn new(tx: AddressSender<A>) -> Addr<A> {
Addr { tx }
}
#[inline]
pub fn connected(&self) -> bool {
self.tx.connected()
}
#[inline]
pub fn do_send<M>(&self, msg: M)
where
M: Message + Send,
M::Result: Send,
A: Handler<M>,
A::Context: ToEnvelope<A, M>,
{
let _ = self.tx.do_send(msg);
}
#[inline]
pub fn send<M>(&self, msg: M) -> Request<A, M>
where
M: Message + Send,
M::Result: Send,
A: Handler<M>,
A::Context: ToEnvelope<A, M>,
{
match self.tx.send(msg) {
Ok(rx) => Request::new(Some(rx), None),
Err(SendError::Full(msg)) => {
Request::new(None, Some((self.tx.clone(), msg)))
}
Err(SendError::Closed(_)) => Request::new(None, None),
}
}
pub fn recipient<M: 'static>(self) -> Recipient<M>
where
A: Handler<M>,
A::Context: ToEnvelope<A, M>,
M: Message + Send,
M::Result: Send,
{
Recipient::new(Box::new(self.tx.clone()))
}
}
impl<A: Actor> Clone for Addr<A> {
fn clone(&self) -> Addr<A> {
Addr {
tx: self.tx.clone(),
}
}
}
pub struct Recipient<M: Message> {
tx: Box<Sender<M>>,
}
unsafe impl<M> Send for Recipient<M>
where
M: Message + Send,
M::Result: Send,
{
}
impl<M> Recipient<M>
where
M: Message + Send,
M::Result: Send,
{
pub(crate) fn new(tx: Box<Sender<M>>) -> Recipient<M> {
Recipient { tx }
}
pub fn do_send(&self, msg: M) -> Result<(), SendError<M>> {
self.tx.do_send(msg)
}
pub fn send(&self, msg: M) -> RecipientRequest<M> {
match self.tx.send(msg) {
Ok(rx) => RecipientRequest::new(Some(rx), None),
Err(SendError::Full(msg)) => {
RecipientRequest::new(None, Some((self.tx.boxed(), msg)))
}
Err(SendError::Closed(_)) => RecipientRequest::new(None, None),
}
}
}
impl<M> Clone for Recipient<M>
where
M: Message + Send,
M::Result: Send,
{
fn clone(&self) -> Recipient<M> {
Recipient {
tx: self.tx.boxed(),
}
}
}