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::fmt;
use crate::validate::InvalidName;
pub use self::{
actor_cell::Context,
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::{ActorId, ActorPath, ActorUri},
};
use crate::actor_ref::BasicActorRef;
use crate::actor_ref::Sender;
use crate::system::SystemMsg;
use crate::Message;
pub type MsgResult<T> = Result<(), MsgError<T>>;
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct MsgError<T> {
pub msg: T,
}
impl<T> MsgError<T> {
pub const fn new(msg: T) -> Self {
Self { msg }
}
}
impl<T> fmt::Display for MsgError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("The actor does not exist. It may have been terminated")
}
}
#[derive(Debug)]
pub struct TryMsgError<T> {
pub msg: T,
}
impl<T> TryMsgError<T> {
pub const fn new(msg: T) -> Self {
Self { msg }
}
}
impl<T> fmt::Display for TryMsgError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Option<ActorRef> is None")
}
}
#[derive(Debug)]
pub enum CreateError {
Panicked,
System,
InvalidName(String),
AlreadyExists(ActorPath),
}
impl fmt::Display for CreateError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::Panicked => {
f.write_str("Failed to create actor. Cause: Actor panicked while starting")
}
Self::System => f.write_str("Failed to create actor. Cause: System failure"),
Self::InvalidName(ref name) => f.write_str(&format!(
"Failed to create actor. Cause: Invalid actor name ({})",
name
)),
Self::AlreadyExists(ref path) => f.write_str(&format!(
"Failed to create actor. Cause: An actor at the same path already exists ({})",
path
)),
}
}
}
impl From<InvalidName> for CreateError {
fn from(err: InvalidName) -> Self {
Self::InvalidName(err.name)
}
}
#[derive(Debug)]
pub struct RestartError;
impl fmt::Display for RestartError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Failed to restart actor. Cause: Actor panicked while starting")
}
}
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 recv(&mut self, ctx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender);
fn sys_recv(&mut self, _ctx: &Context<Self::Msg>, _msg: SystemMsg, _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 supervisor_strategy(&self) -> Strategy {
(**self).supervisor_strategy()
}
fn recv(&mut self, ctx: &Context<Self::Msg>, msg: Self::Msg, sender: Option<BasicActorRef>) {
(**self).recv(ctx, msg, sender)
}
fn sys_recv(
&mut self,
ctx: &Context<Self::Msg>,
msg: SystemMsg,
sender: Option<BasicActorRef>,
) {
(**self).sys_recv(ctx, msg, sender)
}
}
pub trait Receive<Msg: Message> {
type Msg: Message;
fn receive(&mut self, ctx: &Context<Self::Msg>, msg: Msg, sender: Option<BasicActorRef>);
}
pub type BoxActor<Msg> = Box<dyn Actor<Msg = Msg> + Send>;
pub enum Strategy {
Stop,
Restart,
Escalate,
}