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
- The worker starts and calls
run_actor_loop(MyActor::default()) - The main thread sends an
Initpayload viaWorkerHandle::send_init() - The actor’s
init()method is called with the payload - 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§
Sourcetype Init: DeserializeOwned + 'static
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.
Sourcetype Cmd: DeserializeOwned + 'static
type Cmd: DeserializeOwned + 'static
Command type (Main → Worker).
Required Methods§
Sourcefn handle(
&mut self,
cmd: Self::Cmd,
ctx: Context<Self::Evt>,
token: CancellationToken,
) -> impl Future<Output = ()> + '_
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§
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.