Trait simpl_actor::Actor

source ·
pub trait Actor: Sized {
    type Error: Send + 'static;

    // Provided methods
    fn mailbox_size() -> usize { ... }
    async fn on_start(&mut self, id: u64) -> Result<(), Self::Error> { ... }
    async fn on_panic(
        &mut self,
        _err: PanicErr
    ) -> Result<ShouldRestart, Self::Error> { ... }
    async fn on_stop(self, _reason: ActorStopReason) -> Result<(), Self::Error> { ... }
    async fn on_link_died(
        &self,
        id: u64,
        reason: ActorStopReason
    ) -> Result<ShouldStop, Self::Error> { ... }
}
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.

Required Associated Types§

source

type Error: Send + 'static

The error type that can be returned from the actor’s methods.

Provided Methods§

source

fn mailbox_size() -> usize

Specifies the default size of the actor’s mailbox.

This method can be overridden to change the size of the mailbox, affecting how many messages can be queued before backpressure is applied.

§Returns

The size of the mailbox. The default is 64.

source

async fn on_start(&mut self, id: u64) -> Result<(), Self::Error>

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.

source

async fn on_panic( &mut self, _err: PanicErr ) -> Result<ShouldRestart, Self::Error>

Hook that is called when an actor panicked.

This method provides an opportunity to clean up or reset state. It can also determine whether the actor should actually be restarted based on the nature of the error.

§Parameters
  • err: The error that occurred.
§Returns

Whether the actor should be restarted or not.

source

async fn on_stop(self, _reason: ActorStopReason) -> Result<(), Self::Error>

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.
§Returns

A result indicating the successful cleanup or an error if the cleanup process fails.

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 be stopped or not.

Object Safety§

This trait is not object safe.

Implementors§