Trait simpl_actor::Actor
source · pub trait Actor: Sized {
type Ref: ActorRef;
// Provided methods
fn name(&self) -> Cow<'_, str> { ... }
fn actor_ref(&self) -> Self::Ref { ... }
fn try_actor_ref() -> Option<Self::Ref> { ... }
async fn on_start(&mut self) -> Result<(), BoxError> { ... }
async fn on_panic(
&mut self,
err: PanicErr
) -> Result<Option<ActorStopReason>, BoxError> { ... }
async fn on_stop(self, reason: ActorStopReason) -> Result<(), BoxError> { ... }
async fn on_link_died(
&mut self,
id: u64,
reason: ActorStopReason
) -> Result<Option<ActorStopReason>, BoxError> { ... }
}Expand description
Defines the core functionality for actors within an actor-based concurrency model.
Implementors of this trait can leverage asynchronous task execution, lifecycle management hooks, and custom error handling.
This can be implemented with default behaviour using the Actor derive macro.
Methods in this trait that return BoxError will stop the actor with the reason ActorReason::Panicked with the error.
Required Associated Types§
Provided Methods§
sourcefn actor_ref(&self) -> Self::Ref
fn actor_ref(&self) -> Self::Ref
Retrieves a reference to the current actor.
§Panics
This function will panic if called outside the scope of an actor.
§Returns
A reference to the actor of type Self::Ref.
Examples found in repository?
More examples
sourcefn try_actor_ref() -> Option<Self::Ref>
fn try_actor_ref() -> Option<Self::Ref>
Retrieves a reference to the current actor, if available.
§Returns
An Option containing a reference to the actor of type Self::Ref if available,
or None if the actor reference is not available.
sourceasync fn on_start(&mut self) -> Result<(), BoxError>
async fn on_start(&mut self) -> Result<(), BoxError>
Hook that is called before the actor starts processing messages.
This asynchronous method allows for initialization tasks to be performed before the actor starts receiving messages.
§Returns
A result indicating successful initialization or an error if initialization fails.
sourceasync fn on_panic(
&mut self,
err: PanicErr
) -> Result<Option<ActorStopReason>, BoxError>
async fn on_panic( &mut self, err: PanicErr ) -> Result<Option<ActorStopReason>, BoxError>
Hook that is called when an actor panicked or returns an error during an async message.
This method provides an opportunity to clean up or reset state.
It can also determine whether the actor should be killed or if it should continue processing messages by returning None.
§Parameters
err: The error that occurred.
§Returns
Whether the actor should continue processing, or be stopped by returning a stop reason.
sourceasync fn on_stop(self, reason: ActorStopReason) -> Result<(), BoxError>
async fn on_stop(self, reason: ActorStopReason) -> Result<(), BoxError>
Hook that is called before the actor is stopped.
This method allows for cleanup and finalization tasks to be performed before the actor is fully stopped. It can be used to release resources, notify other actors, or complete any final tasks.
§Parameters
reason: The reason why the actor is being stopped.
sourceasync fn on_link_died(
&mut self,
id: u64,
reason: ActorStopReason
) -> Result<Option<ActorStopReason>, BoxError>
async fn on_link_died( &mut self, id: u64, reason: ActorStopReason ) -> Result<Option<ActorStopReason>, BoxError>
Hook that is called when a linked actor dies.
By default, the current actor will be stopped if the reason is anything other than normal.
§Returns
Whether the actor should continue processing, or be stopped by returning a stop reason.