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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::fmt;
use protocol::*;
use actor::{ActorUri, BoxActorProd, ActorCell, CellPublic};
use actor::{Tell, TryTell, SysTell, CreateError, TryMsgError};
#[derive(Clone)]
pub struct ActorRef<Msg: Message> {
pub uri: ActorUri,
pub cell: ActorCell<Msg>
}
impl<Msg: Message> ActorRef<Msg> {
#[doc(hidden)]
pub fn new(uri: &ActorUri, cell: ActorCell<Msg>) -> ActorRef<Msg> {
ActorRef {
uri: uri.clone(),
cell: cell,
}
}
pub fn name(&self) -> &str {
&self.uri.name.as_str()
}
pub fn path(&self) -> &String {
&self.uri.path
}
pub fn is_root(&self) -> bool {
self.cell.is_root()
}
pub fn parent(&self) -> ActorRef<Msg> {
self.cell.parent()
}
pub fn children<'a>(&'a self) -> Box<Iterator<Item = ActorRef<Msg>> + 'a> {
self.cell.children()
}
}
impl<Msg: Message> Tell for ActorRef<Msg> {
type Msg = Msg;
#[allow(unused)]
fn tell<T>(&self,
msg: T,
sender: Option<ActorRef<Self::Msg>>)
where T: Into<ActorMsg<Self::Msg>>
{
let envelope = Envelope {
msg: msg.into(),
sender: sender,
};
let _ = self.cell.send_msg(envelope);
}
}
impl<Msg: Message> TryTell for Option<ActorRef<Msg>> {
type Msg = Msg;
fn try_tell<T>(&self,
msg: T,
sender: Option<ActorRef<Self::Msg>>) -> Result<(), TryMsgError<T>>
where T: Into<ActorMsg<Self::Msg>>
{
match self {
Some(ref actor) => {
let envelope = Envelope {
msg: msg.into(),
sender: sender,
};
let _ = actor.cell.send_msg(envelope);
Ok(())
}
None => Err(TryMsgError::new(msg))
}
}
}
impl<Msg: Message> SysTell for ActorRef<Msg> {
type Msg = Msg;
fn sys_tell(&self,
sys_msg: SystemMsg<Msg>,
sender: Option<ActorRef<Msg>>) {
let envelope = SystemEnvelope {
msg: sys_msg,
sender: sender
};
let _ = self.cell.send_sys_msg(envelope);
}
}
impl<Msg: Message> fmt::Debug for ActorRef<Msg> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Actor[{:?}]", self.uri)
}
}
impl<Msg: Message> fmt::Display for ActorRef<Msg> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Actor[{}://{}#{}]", self.uri.host, self.uri.path, self.uri.uid)
}
}
pub trait ActorRefFactory {
type Msg: Message;
fn actor_of(&self, props: BoxActorProd<Self::Msg>, name: &str) -> Result<ActorRef<Self::Msg>, CreateError>;
fn stop(&self, actor: &ActorRef<Self::Msg>);
}
pub trait TmpActorRefFactory {
type Msg: Message;
fn tmp_actor_of(&self, props: BoxActorProd<Self::Msg>) -> Result<ActorRef<Self::Msg>, CreateError>;
}
impl<Msg: Message> PartialEq for ActorRef<Msg> {
fn eq(&self, other: &ActorRef<Msg>) -> bool {
self.uri.path == other.uri.path
}
}