-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhello_world.rs
42 lines (36 loc) · 1.47 KB
/
hello_world.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
//! This is an example of implementing the classic "Hello World" in the Axiom actor system.
//!
//! Demonstrates
//! * Creating an actor system.
//! * Spawning an actor with a static function handler.
//! * Setting up the current thread to talk to the actor system.
//! * Sending a message to an actor.
//! * Determining the content of a message and acting on the message.
//! * Triggering an actor system shutdown within the actor.
//! * Awaiting the actor system to shut down.
use axiom::prelude::*;
use serde::{Deserialize, Serialize};
/// The messages we will be sending to our actor. All messages must be serializable and
/// deserializable with serde.
#[derive(Serialize, Deserialize)]
enum HelloMessages {
Greet,
}
/// This is the handler that will be used by the actor.
async fn hello(_: (), context: Context, message: Message) -> ActorResult<()> {
if let Some(_msg) = message.content_as::<HelloMessages>() {
println!("Hello World from Actor: {:?}", context.aid);
context.system.trigger_shutdown();
}
Ok(Status::done(()))
}
pub fn main() {
// First we initialize the actor system using the default config.
let config = ActorSystemConfig::default();
let system = ActorSystem::create(config);
// Spawn the actor and send the message.
let aid = system.spawn().with((), hello).unwrap();
aid.send(Message::new(HelloMessages::Greet)).unwrap();
// The actor will trigger shutdown, we just wait for it.
system.await_shutdown(None);
}