pub trait Actor:
Sized
+ Sync
+ Send
+ 'static {
type Msg: Message;
type State: State;
type Arguments: State;
// Required method
fn pre_start(
&self,
myself: ActorRef<Self::Msg>,
args: Self::Arguments,
) -> impl Future<Output = Result<Self::State, ActorProcessingErr>> + Send;
// Provided methods
fn post_start(
&self,
myself: ActorRef<Self::Msg>,
state: &mut Self::State,
) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send { ... }
fn post_stop(
&self,
myself: ActorRef<Self::Msg>,
state: &mut Self::State,
) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send { ... }
fn handle(
&self,
myself: ActorRef<Self::Msg>,
message: Self::Msg,
state: &mut Self::State,
) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send { ... }
fn handle_supervisor_evt(
&self,
myself: ActorRef<Self::Msg>,
message: SupervisionEvent,
state: &mut Self::State,
) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send { ... }
fn spawn(
name: Option<ActorName>,
handler: Self,
startup_args: Self::Arguments,
) -> impl Future<Output = Result<(ActorRef<Self::Msg>, JoinHandle<()>), SpawnErr>> + Send { ... }
fn spawn_linked(
name: Option<ActorName>,
handler: Self,
startup_args: Self::Arguments,
supervisor: ActorCell,
) -> impl Future<Output = Result<(ActorRef<Self::Msg>, JoinHandle<()>), SpawnErr>> + Send { ... }
}Expand description
Actor defines the behavior of an Actor. It specifies the Message type, State type, and all processing logic for the actor
Additionally it aliases the calls for spawn and spawn_linked from
ActorRuntime for convenient startup + lifecycle management
NOTE: All of the implemented trait functions
pre_startpost_startpost_stophandlehandle_serialized(Available withclusterfeature only)handle_supervisor_evt
return a Result<_, ActorProcessingError> where the error type is an alias of [Box<dyn std::error::Error + Send + Sync + ’static>]. This is treated as an “unhandled” error and will terminate the actor + execute necessary supervision patterns. Panics are also captured from the inner functions and wrapped into an Error type, however should an [Err(_)] result from any of these functions the actor will terminate and cleanup.
Required Associated Types§
Required Methods§
Sourcefn pre_start(
&self,
myself: ActorRef<Self::Msg>,
args: Self::Arguments,
) -> impl Future<Output = Result<Self::State, ActorProcessingErr>> + Send
fn pre_start( &self, myself: ActorRef<Self::Msg>, args: Self::Arguments, ) -> impl Future<Output = Result<Self::State, ActorProcessingErr>> + Send
Invoked when an actor is being started by the system.
Any initialization inherent to the actor’s role should be performed here hence why it returns the initial state.
Panics in pre_start do not invoke the
supervision strategy and the actor won’t be started. Actor::spawn
will return an error to the caller
myself- A handle to the ActorCell representing this actorargs- Arguments that are passed in the spawning of the actor which might be necessary to construct the initial state
Returns an initial Actor::State to bootstrap the actor
Provided Methods§
Sourcefn post_start(
&self,
myself: ActorRef<Self::Msg>,
state: &mut Self::State,
) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send
fn post_start( &self, myself: ActorRef<Self::Msg>, state: &mut Self::State, ) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send
Invoked after an actor has started.
Any post initialization can be performed here, such as writing to a log file, emitting metrics.
Panics in post_start follow the supervision strategy.
myself- A handle to the ActorCell representing this actorstate- A mutable reference to the internal actor’s state
Sourcefn post_stop(
&self,
myself: ActorRef<Self::Msg>,
state: &mut Self::State,
) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send
fn post_stop( &self, myself: ActorRef<Self::Msg>, state: &mut Self::State, ) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send
Invoked after an actor has been stopped to perform final cleanup. In the
event the actor is terminated with Signal::Kill or has self-panicked,
post_stop won’t be called.
Panics in post_stop follow the supervision strategy.
myself- A handle to the ActorCell representing this actorstate- A mutable reference to the internal actor’s last known state
Sourcefn handle(
&self,
myself: ActorRef<Self::Msg>,
message: Self::Msg,
state: &mut Self::State,
) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send
fn handle( &self, myself: ActorRef<Self::Msg>, message: Self::Msg, state: &mut Self::State, ) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send
Handle the incoming message from the event processing loop. Unhandled panickes will be captured and sent to the supervisor(s)
myself- A handle to the ActorCell representing this actormessage- The message to processstate- A mutable reference to the internal actor’s state
Sourcefn handle_supervisor_evt(
&self,
myself: ActorRef<Self::Msg>,
message: SupervisionEvent,
state: &mut Self::State,
) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send
fn handle_supervisor_evt( &self, myself: ActorRef<Self::Msg>, message: SupervisionEvent, state: &mut Self::State, ) -> impl Future<Output = Result<(), ActorProcessingErr>> + Send
Handle the incoming supervision event. Unhandled panics will be captured and sent the the supervisor(s). The default supervision behavior is to exit the supervisor on any child exit. To override this behavior, implement this function.
myself- A handle to the ActorCell representing this actormessage- The message to processstate- A mutable reference to the internal actor’s state
Sourcefn spawn(
name: Option<ActorName>,
handler: Self,
startup_args: Self::Arguments,
) -> impl Future<Output = Result<(ActorRef<Self::Msg>, JoinHandle<()>), SpawnErr>> + Send
fn spawn( name: Option<ActorName>, handler: Self, startup_args: Self::Arguments, ) -> impl Future<Output = Result<(ActorRef<Self::Msg>, JoinHandle<()>), SpawnErr>> + Send
Spawn an actor of this type, which is unsupervised, automatically starting
name: A name to give the actor. Useful for global referencing or debug printinghandlerThe implementation of Selfstartup_args: Arguments passed to thepre_startcall of the Actor to facilitate startup and initial state creation
Returns a [Ok((ActorRef, JoinHandle<()>))] upon successful start, denoting the actor reference along with the join handle which will complete when the actor terminates. Returns [Err(SpawnErr)] if the actor failed to start
Sourcefn spawn_linked(
name: Option<ActorName>,
handler: Self,
startup_args: Self::Arguments,
supervisor: ActorCell,
) -> impl Future<Output = Result<(ActorRef<Self::Msg>, JoinHandle<()>), SpawnErr>> + Send
fn spawn_linked( name: Option<ActorName>, handler: Self, startup_args: Self::Arguments, supervisor: ActorCell, ) -> impl Future<Output = Result<(ActorRef<Self::Msg>, JoinHandle<()>), SpawnErr>> + Send
Spawn an actor of this type with a supervisor, automatically starting the actor
name: A name to give the actor. Useful for global referencing or debug printinghandlerThe implementation of Selfstartup_args: Arguments passed to thepre_startcall of the Actor to facilitate startup and initial state creationsupervisor: The ActorCell which is to become the supervisor (parent) of this actor
Returns a [Ok((ActorRef, JoinHandle<()>))] upon successful start, denoting the actor reference along with the join handle which will complete when the actor terminates. Returns [Err(SpawnErr)] if the actor failed to start
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.