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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
pub(crate) mod handler;
pub(crate) mod runner;
mod path;
pub use path::ActorPath;
use thiserror::Error;
use async_trait::async_trait;
use crate::system::{ActorSystem, SystemEvent};
pub struct ActorContext<E: SystemEvent> {
pub path: ActorPath,
pub system: ActorSystem<E>
}
impl<E: SystemEvent> ActorContext<E> {
pub async fn create_child<A: Actor<E>>(&self, name: &str, actor: A) -> Result<ActorRef<E, A>, ActorError> {
let path = self.path.clone() / name;
self.system.create_actor_path(path, actor).await
}
pub async fn get_child<A: Actor<E>>(&self, name: &str) -> Option<ActorRef<E, A>> {
let path = self.path.clone() / name;
self.system.get_actor(&path).await
}
pub async fn get_or_create_child<A, F>(&self, name: &str, actor_fn: F) -> Result<ActorRef<E, A>, ActorError>
where
A: Actor<E>,
F: FnOnce() -> A
{
let path = self.path.clone() / name;
self.system.get_or_create_actor_path(&path, actor_fn).await
}
pub async fn stop_child(&self, name: &str) {
let path = self.path.clone() / name;
self.system.stop_actor(&path).await;
}
}
pub trait Message: Clone + Send + Sync + 'static {
type Response: Send + Sync + 'static;
}
#[async_trait]
pub trait Handler<E: SystemEvent, M: Message>: Send + Sync {
async fn handle(&mut self, msg: M, ctx: &mut ActorContext<E>) -> M::Response;
}
#[async_trait]
pub trait Actor<E: SystemEvent>: Clone + Send + Sync + 'static {
async fn pre_start(&mut self, _ctx: &mut ActorContext<E>) {}
async fn post_stop(&mut self, _ctx: &mut ActorContext<E>) {}
}
#[derive(Clone)]
pub struct ActorRef<E: SystemEvent, A: Actor<E>> {
path: ActorPath,
sender: handler::HandlerRef<E, A>
}
impl<E: SystemEvent, A: Actor<E>> ActorRef<E, A> {
pub fn get_path(&self) -> &ActorPath {
&self.path
}
pub fn tell<M>(&mut self, msg: M) -> Result<(), ActorError>
where
M: Message,
A: Handler<E, M>
{
self.sender.tell(msg)
}
pub async fn ask<M>(&mut self, msg: M) -> Result<M::Response, ActorError>
where
M: Message,
A: Handler<E, M>
{
self.sender.ask(msg).await
}
pub(crate) fn new(path: ActorPath, sender: handler::MailboxSender<E, A>) -> Self {
let handler = handler::HandlerRef::new(sender);
ActorRef {
path,
sender: handler
}
}
}
impl<E: SystemEvent, A: Actor<E>> std::fmt::Debug for ActorRef<E, A> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.path)
}
}
#[derive(Error, Debug)]
pub enum ActorError {
#[error("Actor exists")]
Exists(ActorPath),
#[error("Actor runtime error")]
Runtime(String)
}