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
//! Demonstrates desugaring all convenience methods, showing how you can have full control over
//! all aspects of thespis.
//
use
{
thespis :: { Return, Actor, Message, Handler, Address } ,
thespis_impl :: { DynError, Mailbox, MailboxEnd } ,
async_executors :: { AsyncStd, SpawnHandleExt } ,
std :: { error::Error } ,
futures :: { channel::mpsc, FutureExt, SinkExt } ,
};
#[ derive( Debug, Actor ) ]
//
struct MyActor;
struct Hello( String );
impl Message for Hello
{
type Return = &'static str;
}
impl Handler< Hello > for MyActor
{
fn handle( &mut self, _msg: Hello ) -> Return< <Hello as Message>::Return >
{
async { "world" }.boxed()
}
}
#[async_std::main]
//
async fn main() -> Result< (), Box<dyn Error> >
{
// We can use any channel we want, as long as it has Sink+Clone/Stream.
//
let (tx, rx) = mpsc::channel( 5 );
// We must use a dynamic error for the Sink, so Addr can store it, whichever
// error type our channel actually uses.
//
let tx = Box::new( tx.sink_map_err( |e| Box::new(e) as DynError ) );
// Manually create a mailbox, with a name for the actor and the receiver of
// our channel.
//
let mb = Mailbox::new( "HelloWorld", Box::new(rx) );
// The mailbox gives us the address.
//
let mut addr = mb.addr( tx );
// We didn't need our actor state until now, so if you wanted it to have it's
// own address, that's perfectly feasible to pass in the constructor.
//
let actor = MyActor;
// When the mb starts, it takes ownership of our actor.
//
let mb_handle = AsyncStd.spawn_handle( mb.start(actor) )?;
// call is fallible. The mailbox might be closed (actor panicked), or
// it might close after accepting the message but before sending a response.
// See `ThesErr` docs.
//
let result = addr.call( Hello( "hello".into() ) ).await?;
assert_eq!( "world", dbg!(result) );
// The mailbox stops when all addresses have been dropped. There is also `WeakAddr`
// for addresses that don't keep the mailbox alive.
//
drop( addr );
// When the mailbox stops, we get our actor state back.
//
assert!( matches!( dbg!(mb_handle.await), MailboxEnd::Actor(MyActor) ) );
Ok(())
}