-
Notifications
You must be signed in to change notification settings - Fork 1
/
07_no_runtime.rs
44 lines (34 loc) · 1.11 KB
/
07_no_runtime.rs
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
//! Example of using `messages` without runtime features.
//! In this example we use `smol` to demonstrate some runtime
//! not supported out of the box.
use messages::prelude::*;
struct Ping(usize);
struct MyActor {
count: usize,
}
impl Actor for MyActor {}
#[async_trait]
impl Handler<Ping> for MyActor {
type Result = usize;
async fn handle(&mut self, msg: Ping, _: &Context<Self>) -> Self::Result {
self.count += msg.0;
self.count
}
}
fn main() {
smol::block_on(async {
// Without runtime, we have to manually create the context in order
// to obtain the actor's address.
let context = Context::new();
let mut address = context.address();
let actor_handle = smol::spawn(context.run(MyActor { count: 10 }));
// Send message and get the result.
let res = address.send(Ping(10)).await;
// Check whether actor returned the expected message.
println!("RESULT: {}", res.unwrap() == 20);
// Stop the actor.
address.stop().await;
address.wait_for_stop().await;
actor_handle.await;
});
}