Skip to main content

WorkerActor

Trait WorkerActor 

Source
pub trait WorkerActor: 'static {
    type Init: DeserializeOwned + 'static;
    type Cmd: DeserializeOwned + 'static;
    type Evt: Serialize + 'static;

    // Required method
    fn handle(
        &mut self,
        cmd: Self::Cmd,
        ctx: Context<Self::Evt>,
        token: CancellationToken,
    ) -> impl Future<Output = ()> + '_;

    // Provided method
    fn init(&mut self, _init: Self::Init) { ... }
}
Expand description

Trait for a worker-side actor that processes typed commands.

§Lifecycle

  1. The worker starts and calls run_actor_loop(MyActor::default())
  2. The main thread sends an Init payload via WorkerHandle::send_init()
  3. The actor’s init() method is called with the payload
  4. The command loop begins — all subsequent messages are Cmd

§Type Parameters

  • Init: one-time initialization payload. Use () for no init.
  • Cmd: The command type posted from Main → Worker.
  • Evt: The event type posted from Worker → Main.

Required Associated Types§

Source

type Init: DeserializeOwned + 'static

One-time initialisation payload. The first message from the main thread is always deserialized as this type before the command loop begins.

Use () for workers that need no initialisation.

Source

type Cmd: DeserializeOwned + 'static

Command type (Main → Worker).

Source

type Evt: Serialize + 'static

Event type (Worker → Main).

Required Methods§

Source

fn handle( &mut self, cmd: Self::Cmd, ctx: Context<Self::Evt>, token: CancellationToken, ) -> impl Future<Output = ()> + '_

Handle a single command.

Use ctx to send responses and access the binary payload. Use token to check for cancellation at async yield points. ctx is Clone + 'static — safe to move into spawned tasks.

Provided Methods§

Source

fn init(&mut self, _init: Self::Init)

Called exactly once with the first message, before the command loop.

Default implementation: 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§