Skip to main content

Actor

Trait Actor 

Source
pub trait Actor:
    Send
    + Sync
    + 'static {
    type State: Send + 'static;
    type Message: Send + 'static;

    // Required methods
    fn init(&self, ctx: &Context<Self::Message>) -> Self::State;
    fn handle(
        &self,
        state: &mut Self::State,
        msg: Self::Message,
        ctx: &Context<Self::Message>,
    ) -> Directive;

    // Provided methods
    fn idle(&self, ctx: &Context<Self::Message>) -> Directive { ... }
    fn post_stop(&self) { ... }
    fn config(&self) -> ActorConfig { ... }
}

Required Associated Types§

Source

type State: Send + 'static

The actor’s internal state (owned, not shared).

Source

type Message: Send + 'static

Messages this actor can receive.

Required Methods§

Source

fn init(&self, ctx: &Context<Self::Message>) -> Self::State

Create initial state. Called on start and restart.

Source

fn handle( &self, state: &mut Self::State, msg: Self::Message, ctx: &Context<Self::Message>, ) -> Directive

Handle a single message. This is the core of the actor.

Return Directive to control scheduling:

  • Continue: Process next message immediately
  • Yield: Give other actors a chance to run
  • Park: Sleep until a message arrives
  • Stop: Terminate this actor

Provided Methods§

Source

fn idle(&self, ctx: &Context<Self::Message>) -> Directive

Called when the mailbox is empty.

Use for:

  • Background/periodic work
  • Polling external state
  • Cleanup tasks

Default: Park (sleep until message arrives)

Source

fn post_stop(&self)

Called once after actor stops (always called, even on panic).

Source

fn config(&self) -> ActorConfig

Actor configuration. Override for custom settings.

Implementors§