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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! xtra is a tiny, fast, and safe actor system.
use fmt;
use Future;
use ControlFlow;
use Either;
use ;
pub use ;
pub use Context;
pub use Mailbox;
pub use scoped;
pub use ;
pub use *; // Star export so we don't have to write `cfg` attributes here.
/// This module contains a way to scope a future to the lifetime of an actor, stopping it before it
/// completes if the actor it is associated with stops too.
/// Commonly used types from xtra
/// This module contains types representing the strength of an address's reference counting, which
/// influences whether the address will keep the actor alive for as long as it lives.
/// Provides a default implementation of the [`Actor`] trait for the given type with a [`Stop`](Actor::Stop) type of `()` and empty lifecycle functions.
///
/// The [`Actor`] custom derive takes away some boilerplate for a standard actor:
///
/// ```rust
/// #[derive(xtra::Actor)]
/// pub struct MyActor;
/// #
/// # fn assert_actor<T: xtra::Actor>() { }
/// #
/// # fn main() {
/// # assert_actor::<MyActor>()
/// # }
/// ```
/// This macro will generate the following [`Actor`] implementation:
///
/// ```rust,no_run
/// # use xtra::prelude::*;
/// pub struct MyActor;
///
///
/// impl xtra::Actor for MyActor {
/// type Stop = ();
///
/// async fn stopped(self) { }
/// }
/// ```
///
/// Please note that implementing the [`Actor`] trait is still very easy and this macro purposely does not support a plethora of usecases but is meant to handle the most common ones.
/// For example, whilst it does support actors with type parameters, lifetimes are entirely unsupported.
pub use Actor;
use crateMessage;
/// Defines that an [`Actor`] can handle a given message `M`.
///
/// # Example
///
/// ```rust
/// # use xtra::prelude::*;
/// # struct MyActor;
/// # impl Actor for MyActor {type Stop = (); async fn stopped(self) -> Self::Stop {} }
/// struct Msg;
///
///
/// impl Handler<Msg> for MyActor {
/// type Return = u32;
///
/// async fn handle(&mut self, message: Msg, ctx: &mut Context<Self>) -> u32 {
/// 20
/// }
/// }
///
/// fn main() {
/// # #[cfg(feature = "smol")]
/// smol::block_on(async {
/// let addr = xtra::spawn_smol(MyActor, Mailbox::unbounded());
/// assert_eq!(addr.send(Msg).await, Ok(20));
/// })
/// }
/// ```
/// An actor which can handle message one at a time. Actors can only be
/// communicated with by sending messages through their [`Address`]es.
/// They can modify their private state, respond to messages, and spawn other actors. They can also
/// stop themselves through their [`Context`] by calling [`Context::stop_self`].
/// This will result in any attempt to send messages to the actor in future failing.
///
/// # Example
///
/// ```rust
/// # use xtra::prelude::*;
/// # use std::time::Duration;
/// struct MyActor;
///
///
/// impl Actor for MyActor {
/// type Stop = ();
/// async fn started(&mut self, ctx: &Mailbox<Self>) -> Result<(), Self::Stop> {
/// println!("Started!");
///
/// Ok(())
/// }
///
/// async fn stopped(self) -> Self::Stop {
/// println!("Finally stopping.");
/// }
/// }
///
/// struct Goodbye;
///
///
/// impl Handler<Goodbye> for MyActor {
/// type Return = ();
///
/// async fn handle(&mut self, _: Goodbye, ctx: &mut Context<Self>) {
/// println!("Goodbye!");
/// ctx.stop_all();
/// }
/// }
///
/// // Will print "Started!", "Goodbye!", and then "Finally stopping."
/// # #[cfg(feature = "smol")]
/// smol::block_on(async {
/// let addr = xtra::spawn_smol(MyActor, Mailbox::unbounded());
/// addr.send(Goodbye).await;
///
/// smol::Timer::after(Duration::from_secs(1)).await; // Give it time to run
/// })
/// ```
///
/// For longer examples, see the `examples` directory.
/// An error related to the actor system
/// Run the provided actor.
///
/// This is the primary event loop of an actor which takes messages out of the mailbox and hands
/// them to the actor.
pub async
/// Yields to the manager to handle one message, returning the actor should be shut down or not.
pub async
/// Handle any incoming messages for the actor while running a given future. This is similar to
/// [`join`], but will exit if the actor is stopped, returning the future. Returns
/// `Ok` with the result of the future if it was successfully completed, or `Err` with the
/// future if the actor was stopped before it could complete. It is analagous to
/// [`futures::select`](futures_util::future::select).
///
/// ## Example
///
/// ```rust
/// # use std::time::Duration;
/// use futures_util::future::Either;
/// # use xtra::prelude::*;
/// # use smol::future;
/// # struct MyActor;
/// # impl Actor for MyActor { type Stop = (); async fn stopped(self) {} }
///
/// struct Stop;
/// struct Selecting;
///
/// impl Handler<Stop> for MyActor {
/// type Return = ();
///
/// async fn handle(&mut self, _msg: Stop, ctx: &mut Context<Self>) {
/// ctx.stop_self();
/// }
/// }
///
/// impl Handler<Selecting> for MyActor {
/// type Return = bool;
///
/// async fn handle(&mut self, _msg: Selecting, ctx: &mut Context<Self>) -> bool {
/// // Actor is still running, so this will return Either::Left
/// match xtra::select(ctx.mailbox(), self, future::ready(1)).await {
/// Either::Left(ans) => println!("Answer is: {}", ans),
/// Either::Right(_) => panic!("How did we get here?"),
/// };
///
/// let addr = ctx.mailbox().address();
/// let select = xtra::select(ctx.mailbox(), self, future::pending::<()>());
/// let _ = addr.send(Stop).detach().await;
///
/// // Actor is stopping, so this will return Err, even though the future will
/// // usually never complete.
/// matches!(select.await, Either::Right(_))
/// }
/// }
///
/// # #[cfg(feature = "smol")]
/// smol::block_on(async {
/// let addr = xtra::spawn_smol(MyActor, Mailbox::unbounded());
/// assert!(addr.is_connected());
/// assert_eq!(addr.send(Selecting).await, Ok(true)); // Assert that the select did end early
/// })
/// ```
pub async
/// Joins on a future by handling all incoming messages whilst polling it. The future will
/// always be polled to completion, even if the actor is stopped. If the actor is stopped,
/// handling of messages will cease, and only the future will be polled. It is somewhat
/// analagous to [`futures::join`](futures_util::future::join), but it will not wait for the incoming stream of messages
/// from addresses to end before returning - it will return as soon as the provided future does.
///
/// ## Example
///
/// ```rust
/// # use std::time::Duration;
/// use futures_util::FutureExt;
/// # use xtra::prelude::*;
/// # use smol::future;
/// # struct MyActor;
/// # impl Actor for MyActor { type Stop = (); async fn stopped(self) {} }
///
/// struct Stop;
/// struct Joining;
///
/// impl Handler<Stop> for MyActor {
/// type Return = ();
///
/// async fn handle(&mut self, _msg: Stop, ctx: &mut Context<Self>) {
/// ctx.stop_self();
/// }
/// }
///
/// impl Handler<Joining> for MyActor {
/// type Return = bool;
///
/// async fn handle(&mut self, _msg: Joining, ctx: &mut Context<Self>) -> bool {
/// let addr = ctx.mailbox().address();
/// let join = xtra::join(ctx.mailbox(), self, future::ready::<()>(()));
/// let _ = addr.send(Stop).detach().await;
///
/// // Actor is stopping, but the join should still evaluate correctly
/// join.now_or_never().is_some()
/// }
/// }
///
/// # #[cfg(feature = "smol")]
/// smol::block_on(async {
/// let addr = xtra::spawn_smol(MyActor, Mailbox::unbounded());
/// assert!(addr.is_connected());
/// assert_eq!(addr.send(Joining).await, Ok(true)); // Assert that the join did evaluate the future
/// })
/// ```
pub async