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:
- Closes the mailbox (no new messages accepted)
- Calls
on_stopfor cleanup - Returns the actor state to the caller via
ActorHandle::join