-
Notifications
You must be signed in to change notification settings - Fork 1
/
02_notify.rs
87 lines (72 loc) · 2.25 KB
/
02_notify.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
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
//! More comprehensive example that aims to show more of `message`
//! crate functionality.
//!
//! In this example, both `Handler` and `Notifiable` traits are implemented,
//! as well as additional methods of an `Actor` trait.
use messages::prelude::*;
#[derive(Debug, Default)]
pub struct Service {
value: u64,
}
#[async_trait]
impl Actor for Service {
// `started` method will be invoked *before* the first message
// will be received.
async fn started(&mut self) {
println!("Service was started");
}
// `stopping` method will be invoked when the actor will be requested
// to stop its execution.
async fn stopping(&mut self) -> ActorAction {
println!("Service is stopping");
// We could've ignored the stop request by returning `ActorAction::KeepRunning`.
ActorAction::Stop
}
// `stopped` method will be invoked once actor is actually stopped.
fn stopped(&mut self) {
println!("Service has stopped");
}
}
// Type that we will use to send messages.
#[derive(Debug)]
pub struct Request(pub u64);
#[async_trait]
impl Handler<Request> for Service {
type Result = u64;
async fn handle(&mut self, input: Request, _: &Context<Self>) -> u64 {
self.value + input.0
}
}
// Type that we will use for notifications.
#[derive(Debug)]
pub struct Notification(pub u64);
// Unlike `Handler`, `Notifiable` trait doesn't have output.
// It only serves one purpose: deliver a message to an actor.
// No response is expected.
#[async_trait]
impl Notifiable<Notification> for Service {
async fn notify(&mut self, input: Notification, _: &Context<Self>) {
self.value = input.0;
}
}
impl Service {
pub fn new() -> Self {
Self::default()
}
}
#[tokio::main]
async fn main() {
// Start a service.
let mut address = Service::new().spawn();
// Send a notification.
address.notify(Notification(10)).await.unwrap();
// Send a request and receive a response.
let response: u64 = address.send(Request(1)).await.unwrap();
assert_eq!(response, 11);
// Stop service.
address.stop().await;
// Wait for service to stop.
address.wait_for_stop().await;
// Ensure that actor is not running anymore.
assert!(!address.connected());
}