#![allow(unused_variables)]
pub(crate) mod actor_cell;
pub(crate) mod actor_ref;
pub(crate) mod channel;
pub(crate) mod macros;
pub(crate) mod props;
pub(crate) mod selection;
pub(crate) mod uri;
use std::{error::Error, fmt};
use crate::validate::InvalidName;
pub use self::{
actor_cell::Context,
actor_ref::{
ActorRef, ActorRefFactory, ActorReference, BasicActorRef, BoxedTell, Sender, Tell,
TmpActorRefFactory,
},
channel::{
channel, All, Channel, ChannelMsg, ChannelRef, DLChannelMsg, DeadLetter, EventsChannel,
Publish, Subscribe, SysTopic, Topic, Unsubscribe, UnsubscribeAll,
},
macros::actor,
props::{ActorArgs, ActorFactory, ActorFactoryArgs, ActorProducer, BoxActorProd, Props},
selection::{ActorSelection, ActorSelectionFactory},
uri::{ActorPath, ActorUri},
};
use crate::{system::SystemMsg, Message};
#[allow(unused)]
pub type MsgResult<T> = Result<(), MsgError<T>>;
#[doc(hidden)]
#[derive(Clone)]
pub struct MsgError<T> {
pub msg: T,
}
impl<T> MsgError<T> {
pub fn new(msg: T) -> Self {
MsgError { msg }
}
}
impl<T> Error for MsgError<T> {
fn description(&self) -> &str {
"The actor does not exist. It may have been terminated"
}
}
impl<T> fmt::Display for MsgError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_string())
}
}
impl<T> fmt::Debug for MsgError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_string())
}
}
pub struct TryMsgError<T> {
pub msg: T,
}
impl<T> TryMsgError<T> {
pub fn new(msg: T) -> Self {
TryMsgError { msg }
}
}
impl<T> Error for TryMsgError<T> {
fn description(&self) -> &str {
"Option<ActorRef> is None"
}
}
impl<T> fmt::Display for TryMsgError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_string())
}
}
impl<T> fmt::Debug for TryMsgError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_string())
}
}
#[derive(Debug)]
pub enum CreateError {
Panicked,
System,
InvalidName(String),
AlreadyExists(ActorPath),
}
impl Error for CreateError {
fn description(&self) -> &str {
match *self {
CreateError::Panicked => "Failed to create actor. Cause: Actor panicked while starting",
CreateError::System => "Failed to create actor. Cause: System failure",
CreateError::InvalidName(_) => "Failed to create actor. Cause: Invalid actor name",
CreateError::AlreadyExists(_) => {
"Failed to create actor. Cause: An actor at the same path already exists"
}
}
}
}
impl fmt::Display for CreateError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl From<InvalidName> for CreateError {
fn from(err: InvalidName) -> CreateError {
CreateError::InvalidName(err.name)
}
}
pub struct RestartError;
impl Error for RestartError {
fn description(&self) -> &str {
"Failed to restart actor. Cause: Actor panicked while starting"
}
}
impl fmt::Display for RestartError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_string())
}
}
impl fmt::Debug for RestartError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_string())
}
}
pub trait Actor: Send + 'static {
type Msg: Message;
fn pre_start(&mut self, ctx: &Context<Self::Msg>) {}
fn post_start(&mut self, ctx: &Context<Self::Msg>) {}
fn post_stop(&mut self) {}
fn supervisor_strategy(&self) -> Strategy {
Strategy::Restart
}
fn sys_recv(&mut self, ctx: &Context<Self::Msg>, msg: SystemMsg, sender: Sender) {}
fn recv(&mut self, ctx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender);
}
impl<A: Actor + ?Sized> Actor for Box<A> {
type Msg = A::Msg;
fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
(**self).pre_start(ctx);
}
fn post_start(&mut self, ctx: &Context<Self::Msg>) {
(**self).post_start(ctx)
}
fn post_stop(&mut self) {
(**self).post_stop()
}
fn sys_recv(
&mut self,
ctx: &Context<Self::Msg>,
msg: SystemMsg,
sender: Option<BasicActorRef>,
) {
(**self).sys_recv(ctx, msg, sender)
}
fn supervisor_strategy(&self) -> Strategy {
(**self).supervisor_strategy()
}
fn recv(&mut self, ctx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender) {
(**self).recv(ctx, msg, sender)
}
}
pub trait Receive<Msg: Message> {
type Msg: Message;
fn receive(&mut self, ctx: &Context<Self::Msg>, msg: Msg, sender: Sender);
}
pub type BoxActor<Msg> = Box<dyn Actor<Msg = Msg> + Send>;
pub enum Strategy {
Stop,
Restart,
Escalate,
}