1#![allow(unused_variables)]
2pub(crate) mod actor_cell;
3pub(crate) mod actor_ref;
4pub(crate) mod channel;
5pub(crate) mod macros;
6pub(crate) mod props;
7pub(crate) mod selection;
8pub(crate) mod uri;
9
10use std::{error::Error, fmt};
11
12use crate::validate::InvalidName;
13
14pub use self::{
16 actor_cell::Context,
17 actor_ref::{
18 ActorRef, ActorRefFactory, ActorReference, BasicActorRef, BoxedTell, Sender, Tell,
19 TmpActorRefFactory,
20 },
21 channel::{
22 channel, All, Channel, ChannelMsg, ChannelRef, DLChannelMsg, DeadLetter, EventsChannel,
23 Publish, Subscribe, SysTopic, Topic, Unsubscribe, UnsubscribeAll,
24 },
25 macros::actor,
26 props::{ActorArgs, ActorFactory, ActorFactoryArgs, ActorProducer, BoxActorProd, Props},
27 selection::{ActorSelection, ActorSelectionFactory},
28 uri::{ActorPath, ActorUri},
29};
30
31use crate::{system::SystemMsg, Message};
32
33#[allow(unused)]
34pub type MsgResult<T> = Result<(), MsgError<T>>;
35
36#[doc(hidden)]
38#[derive(Clone)]
39pub struct MsgError<T> {
40 pub msg: T,
41}
42
43impl<T> MsgError<T> {
44 pub fn new(msg: T) -> Self {
45 MsgError { msg }
46 }
47}
48
49impl<T> Error for MsgError<T> {
50 fn description(&self) -> &str {
51 "The actor does not exist. It may have been terminated"
52 }
53}
54
55impl<T> fmt::Display for MsgError<T> {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 f.write_str(&self.to_string())
58 }
59}
60
61impl<T> fmt::Debug for MsgError<T> {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 f.write_str(&self.to_string())
64 }
65}
66
67pub struct TryMsgError<T> {
69 pub msg: T,
70}
71
72impl<T> TryMsgError<T> {
73 pub fn new(msg: T) -> Self {
74 TryMsgError { msg }
75 }
76}
77
78impl<T> Error for TryMsgError<T> {
79 fn description(&self) -> &str {
80 "Option<ActorRef> is None"
81 }
82}
83
84impl<T> fmt::Display for TryMsgError<T> {
85 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 f.write_str(&self.to_string())
87 }
88}
89
90impl<T> fmt::Debug for TryMsgError<T> {
91 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92 f.write_str(&self.to_string())
93 }
94}
95
96#[derive(Debug)]
98pub enum CreateError {
99 Panicked,
100 System,
101 InvalidName(String),
102 AlreadyExists(ActorPath),
103}
104
105impl Error for CreateError {
106 fn description(&self) -> &str {
107 match *self {
108 CreateError::Panicked => "Failed to create actor. Cause: Actor panicked while starting",
109 CreateError::System => "Failed to create actor. Cause: System failure",
110 CreateError::InvalidName(_) => "Failed to create actor. Cause: Invalid actor name",
111 CreateError::AlreadyExists(_) => {
112 "Failed to create actor. Cause: An actor at the same path already exists"
113 }
114 }
115 }
116}
117
118impl fmt::Display for CreateError {
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120 fmt::Debug::fmt(self, f)
121 }
122}
123
124impl From<InvalidName> for CreateError {
125 fn from(err: InvalidName) -> CreateError {
126 CreateError::InvalidName(err.name)
127 }
128}
129
130pub struct RestartError;
132
133impl Error for RestartError {
134 fn description(&self) -> &str {
135 "Failed to restart actor. Cause: Actor panicked while starting"
136 }
137}
138
139impl fmt::Display for RestartError {
140 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141 f.write_str(&self.to_string())
142 }
143}
144
145impl fmt::Debug for RestartError {
146 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147 f.write_str(&self.to_string())
148 }
149}
150
151pub trait Actor: Send + 'static {
152 type Msg: Message;
153
154 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {}
162
163 fn post_start(&mut self, ctx: &Context<Self::Msg>) {}
170
171 fn post_stop(&mut self) {}
173
174 fn supervisor_strategy(&self) -> Strategy {
176 Strategy::Restart
177 }
178
179 fn sys_recv(&mut self, ctx: &Context<Self::Msg>, msg: SystemMsg, sender: Sender) {}
184
185 fn recv(&mut self, ctx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender);
190}
191
192impl<A: Actor + ?Sized> Actor for Box<A> {
193 type Msg = A::Msg;
194
195 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
196 (**self).pre_start(ctx);
197 }
198
199 fn post_start(&mut self, ctx: &Context<Self::Msg>) {
200 (**self).post_start(ctx)
201 }
202
203 fn post_stop(&mut self) {
204 (**self).post_stop()
205 }
206
207 fn sys_recv(
208 &mut self,
209 ctx: &Context<Self::Msg>,
210 msg: SystemMsg,
211 sender: Option<BasicActorRef>,
212 ) {
213 (**self).sys_recv(ctx, msg, sender)
214 }
215
216 fn supervisor_strategy(&self) -> Strategy {
217 (**self).supervisor_strategy()
218 }
219
220 fn recv(&mut self, ctx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender) {
221 (**self).recv(ctx, msg, sender)
222 }
223}
224
225pub trait Receive<Msg: Message> {
284 type Msg: Message;
285
286 fn receive(&mut self, ctx: &Context<Self::Msg>, msg: Msg, sender: Sender);
291}
292
293pub type BoxActor<Msg> = Box<dyn Actor<Msg = Msg> + Send>;
295
296pub enum Strategy {
300 Stop,
302
303 Restart,
305
306 Escalate,
308}