Skip to main content

Actor

Trait Actor 

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

    // Required methods
    fn init(
        init: Self::Init,
        ctx: &mut Context<Self>,
    ) -> impl Future<Output = Self> + Send;
    fn handle_message(
        &mut self,
        msg: Self::Message,
        ctx: &mut Context<Self>,
    ) -> impl Future<Output = ()> + Send;

    // Provided method
    fn on_stop(
        &mut self,
        _ctx: &mut Context<Self>,
    ) -> impl Future<Output = ()> + Send { ... }
}
Expand description

An asynchronous, Send-safe actor.

Implement this for actors whose state is Send. The runtime calls init once, then loops calling handle_message for each message, and finally calls on_stop before the task exits.

For !Send state use LocalActor. To also receive stream events use StreamActor.

Required Associated Types§

Source

type Init: Send + 'static

Initialization parameter. Must be Send + 'static since it may cross task boundaries.

Required Methods§

Source

fn init( init: Self::Init, ctx: &mut Context<Self>, ) -> impl Future<Output = Self> + Send

Construct the actor. Called once before the message loop starts.

Source

fn handle_message( &mut self, msg: Self::Message, ctx: &mut Context<Self>, ) -> impl Future<Output = ()> + Send

Process a single incoming message.

Provided Methods§

Source

fn on_stop( &mut self, _ctx: &mut Context<Self>, ) -> impl Future<Output = ()> + Send

Called after the message loop exits. Default is a no-op.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§