Skip to main content

Actor

Trait Actor 

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

    // Required method
    fn handle(
        &mut self,
        cx: &Cx,
        msg: Self::Message,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>;

    // Provided methods
    fn on_start(
        &mut self,
        _cx: &Cx,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> { ... }
    fn on_stop(
        &mut self,
        _cx: &Cx,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> { ... }
}
Expand description

A message-driven actor that processes messages from a bounded mailbox.

Actors are the unit of stateful, message-driven concurrency. Each actor:

  • Owns mutable state (self)
  • Receives messages sequentially (no data races)
  • Runs inside a region (structured lifetime)

§Cancel Safety

When an actor is cancelled (region close, explicit abort), the runtime:

  1. Closes the mailbox (no new messages accepted)
  2. Calls on_stop for cleanup
  3. Returns the actor state to the caller via ActorHandle::join

Required Associated Types§

Source

type Message: Send + 'static

The type of messages this actor can receive.

Required Methods§

Source

fn handle( &mut self, cx: &Cx, msg: Self::Message, ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>

Handle a single message.

This is called sequentially for each message in the mailbox. The actor has exclusive access to its state during handling.

Provided Methods§

Source

fn on_start( &mut self, _cx: &Cx, ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>

Called once when the actor starts, before processing any messages.

Use this for initialization that requires the capability context. The default implementation does nothing.

Source

fn on_stop(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>

Called once when the actor is stopping, after the mailbox is drained.

Use this for cleanup. The default implementation does nothing.

Implementors§