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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! Types related the `ActorRef` Remote Procedure Call (RPC) mechanism.
//!
//! RPC is implemented by sending a [`RpcMessage`] to the actor, which contains
//! the request message and a [`RpcResponse`]. The `RpcResponse` allows the
//! receiving actor to send back a response to the sending actor.
//!
//! To support RPC the receiving actor needs to implement
//! [`From`]`<`[`RpcMessage`]`<Req, Res>>`, where `Req` is the type of the
//! request message and `Res` the type of the response. This can be done easily
//! by using the [`from_message`] macro. The RPC message can then be received
//! like any other message.
//!
//! The sending actor needs to call [`ActorRef::rpc`] with the correct request
//! type. That will return an [`Rpc`] [`Future`] which returns the response to
//! the call, or [`RpcError`] in case of an error.
//!
//! [`from_message`]: crate::from_message
//!
//! # Examples
//!
//! Using RPC to communicate with another actor.
//!
//! ```
//! # #![feature(never_type)]
//! #
//! use heph::actor;
//! use heph::actor_ref::{ActorRef, RpcMessage};
//! use heph_rt::{self as rt, ThreadLocal};
//!
//! /// Message type for [`counter`].
//! struct Add(RpcMessage<usize, usize>);
//!
//! /// Required to support RPC.
//! impl From<RpcMessage<usize, usize>> for Add {
//! fn from(msg: RpcMessage<usize, usize>) -> Add {
//! Add(msg)
//! }
//! }
//!
//! /// Receiving actor of the RPC.
//! async fn counter(mut ctx: actor::Context<Add, ThreadLocal>) {
//! // State of the counter.
//! let mut count: usize = 0;
//! // Receive a message like normal.
//! while let Ok(Add(RpcMessage { request, response })) = ctx.receive_next().await {
//! count += request;
//! // Send back the current state, ignoring any errors.
//! let _ = response.respond(count);
//! }
//! }
//!
//! /// Sending actor of the RPC.
//! async fn requester(_: actor::Context<!, ThreadLocal>, actor_ref: ActorRef<Add>) {
//! // Make the procedure call.
//! let response = actor_ref.rpc(10).await;
//! # assert!(response.is_ok());
//! match response {
//! // We got a response.
//! Ok(count) => println!("Current count: {}", count),
//! // Actor failed to respond.
//! Err(err) => eprintln!("Counter didn't reply: {}", err),
//! }
//! }
//!
//! # fn main() -> Result<(), rt::Error> {
//! # use heph::supervisor::NoSupervisor;
//! # use heph_rt::Runtime;
//! # use heph_rt::spawn::ActorOptions;
//! # let mut runtime = Runtime::new()?;
//! # runtime.run_on_workers(|mut runtime_ref| -> Result<(), !> {
//! # let counter = counter as fn(_) -> _;
//! # let actor_ref = runtime_ref.spawn_local(NoSupervisor, counter, (), ActorOptions::default());
//! #
//! # let requester = requester as fn(_, _) -> _;
//! # runtime_ref.spawn_local(NoSupervisor, requester, actor_ref, ActorOptions::default());
//! # Ok(())
//! # })?;
//! # runtime.start()
//! # }
//! ```
//!
//! Supporting multiple procedure within the same actor is possible by making
//! the message an `enum` as the example below shows. Furthermore synchronous
//! actors are supported.
//!
//! ```
//! # #![feature(never_type)]
//! #
//! use heph::actor::{self, SyncContext};
//! use heph::actor_ref::{ActorRef, RpcMessage};
//! use heph::from_message;
//! use heph_rt::{self as rt, ThreadLocal};
//!
//! /// Message type for [`counter`].
//! enum Message {
//! /// Increase the counter, returning the current state.
//! Add(RpcMessage<usize, usize>),
//! /// Get the current state of the counter.
//! Get(RpcMessage<(), usize>),
//! }
//!
//! // Implement the `From` trait for `Message`.
//! from_message!(Message::Add(usize) -> usize);
//! from_message!(Message::Get(()) -> usize);
//!
//! /// Receiving synchronous actor of the RPC.
//! fn counter<RT>(mut ctx: SyncContext<Message, RT>) {
//! // State of the counter.
//! let mut count: usize = 0;
//!
//! // Receive messages in a loop.
//! while let Ok(msg) = ctx.receive_next() {
//! match msg {
//! Message::Add(RpcMessage { request, response }) => {
//! count += request;
//! // Send back the current state, ignoring any errors.
//! let _ = response.respond(count);
//! },
//! Message::Get(RpcMessage { response, .. }) => {
//! // Send back the current state, ignoring any errors.
//! let _ = response.respond(count);
//! },
//! }
//! }
//! }
//!
//! /// Sending actor of the RPC.
//! async fn requester(_: actor::Context<!, ThreadLocal>, actor_ref: ActorRef<Message>) {
//! // Increase the counter by ten.
//! // NOTE: do handle the errors correctly in practice, this is just an
//! // example.
//! let count = actor_ref.rpc(10).await.unwrap();
//! println!("Increased count to {}", count);
//!
//! // Retrieve the current count.
//! let count = actor_ref.rpc(()).await.unwrap();
//! # assert_eq!(count, 10);
//! println!("Current count {}", count);
//! }
//!
//! # fn main() -> Result<(), rt::Error> {
//! # use heph::supervisor::NoSupervisor;
//! # use heph_rt::Runtime;
//! # use heph_rt::spawn::{ActorOptions, SyncActorOptions};
//! #
//! # let mut runtime = Runtime::new()?;
//! # let counter = counter as fn(_) -> _;
//! # let options = SyncActorOptions::default();
//! # let actor_ref = runtime.spawn_sync_actor(NoSupervisor, counter, (), options)?;
//! # runtime.run_on_workers(move |mut runtime_ref| -> Result<(), !> {
//! # let requester = requester as fn(_, _) -> _;
//! # runtime_ref.spawn_local(NoSupervisor, requester, actor_ref, ActorOptions::default());
//! # Ok(())
//! # })?;
//! # runtime.start()
//! # }
//! ```
use Error;
use fmt;
use Future;
use Pin;
use ;
use ;
use crate;
/// [`Future`] that resolves to a Remote Procedure Call (RPC) response.
///
/// Created by [`ActorRef::rpc`].
/// Error returned by [`Rpc`].
/// Message type that holds an RPC request.
///
/// It holds both the request (`Req`) and the way to respond [`RpcResponse`].
/// Structure to respond to an [`Rpc`] request.