Skip to main content

Actor

Trait Actor 

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

    // Required method
    fn handle(&mut self, cmd: Self::Command) -> impl Future<Output = ()> + Send;

    // Provided methods
    fn on_idle(&mut self) -> impl Future<Output = ()> + Send { ... }
    fn on_shutdown(&mut self) -> impl Future<Output = ()> + Send { ... }
}
Available on crate feature concurrency only.
Expand description

An actor owns mutable state and processes commands sequentially.

Implementations pick a Command type (typically an enum with embedded oneshot::Sender<Reply> fields for request-response).

Required Associated Types§

Source

type Command: Send + 'static

Command type received from the channel.

Required Methods§

Source

fn handle(&mut self, cmd: Self::Command) -> impl Future<Output = ()> + Send

Process one command. Called in receive order.

Provided Methods§

Source

fn on_idle(&mut self) -> impl Future<Output = ()> + Send

Called every idle_interval when no commands are pending. Default: no-op. Useful for periodic state maintenance (cleanup, metrics emit, etc.) without a separate timer task.

Source

fn on_shutdown(&mut self) -> impl Future<Output = ()> + Send

Called once after shutdown is signalled (or all senders dropped) and the in-flight command finishes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§