-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickstart.rs
66 lines (61 loc) · 1.66 KB
/
quickstart.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
use std::error::Error;
use std::time::Duration;
use tyra::prelude::*;
// define an `ActorMessage` that can be sent to `Actors` that implement the corresponding `Handler<T>`
struct TestMessage {}
impl TestMessage {
pub fn new() -> Self {
Self {}
}
}
impl ActorMessage for TestMessage {}
// define the actual `Actor` that should process messages
struct TestActor {}
impl TestActor {
pub fn new() -> Self {
Self {}
}
}
impl Actor for TestActor {}
// define a factory that creates the `Actor` for us
struct TestActorFactory {}
impl TestActorFactory {
pub fn new() -> Self {
Self {}
}
}
impl ActorFactory<TestActor> for TestActorFactory {
fn new_actor(
&mut self,
_context: ActorContext<TestActor>,
) -> Result<TestActor, Box<dyn Error>> {
Ok(TestActor::new())
}
}
// implement our message for the `Actor`
impl Handler<TestMessage> for TestActor {
fn handle(
&mut self,
_msg: TestMessage,
context: &ActorContext<Self>,
) -> Result<ActorResult, Box<dyn Error>> {
println!("HELLO WORLD!");
context.system.stop(Duration::from_millis(1000));
Ok(ActorResult::Ok)
}
}
fn main() {
// generate config
let actor_config = TyraConfig::new().unwrap();
// start system with config
let actor_system = ActorSystem::new(actor_config);
// create actor on the system
let actor = actor_system
.builder()
.spawn("test", TestActorFactory::new())
.unwrap();
// send a message to the actor
actor.send(TestMessage::new()).unwrap();
// wait for the system to stop
std::process::exit(actor_system.await_shutdown());
}