Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Handler Traits

Handler traits enable type-erased message sending, allowing different actor types that handle the same message to be stored in a unified collection. This is essential for building systems where you need to broadcast messages to heterogeneous actors.

Overview

rsActor provides four handler traits:

TraitReferenceMessage Pattern
TellHandler<M>StrongFire-and-forget
AskHandler<M, R>StrongRequest-response
WeakTellHandler<M>WeakFire-and-forget
WeakAskHandler<M, R>WeakRequest-response

Strong handlers keep actors alive. Weak handlers do not prevent actors from being dropped.

TellHandler — Fire-and-Forget

Store different actor types that handle the same message in one collection:

#![allow(unused)]
fn main() {
use rsactor::{TellHandler, ActorRef, spawn, message_handlers, Actor};

// Two different actors that both handle PingMsg
#[derive(Actor)]
struct ActorA;
#[derive(Actor)]
struct ActorB;

struct PingMsg;

#[message_handlers]
impl ActorA {
    #[handler]
    async fn handle_ping(&mut self, _: PingMsg, _: &ActorRef<Self>) -> () {}
}

#[message_handlers]
impl ActorB {
    #[handler]
    async fn handle_ping(&mut self, _: PingMsg, _: &ActorRef<Self>) -> () {}
}

// Store both in a single collection
let (ref_a, _) = spawn::<ActorA>(ActorA);
let (ref_b, _) = spawn::<ActorB>(ActorB);

let handlers: Vec<Box<dyn TellHandler<PingMsg>>> = vec![
    (&ref_a).into(),  // From<&ActorRef<T>> — clones the reference
    ref_b.into(),     // From<ActorRef<T>> — moves ownership
];

// Broadcast to all handlers
for handler in &handlers {
    handler.tell(PingMsg).await?;
}
}

AskHandler — Request-Response

For actors that return the same reply type for a message:

#![allow(unused)]
fn main() {
use rsactor::AskHandler;

struct GetStatus;

// Both actors return String for GetStatus
let handlers: Vec<Box<dyn AskHandler<GetStatus, String>>> = vec![
    (&actor_a).into(),
    (&actor_b).into(),
];

for handler in &handlers {
    let status = handler.ask(GetStatus).await?;
    println!("Status: {}", status);
}
}

Weak Handlers

Weak handlers do not keep actors alive. You must upgrade() before sending messages:

#![allow(unused)]
fn main() {
use rsactor::{WeakTellHandler, ActorRef};

let weak_handlers: Vec<Box<dyn WeakTellHandler<PingMsg>>> = vec![
    ActorRef::downgrade(&actor_a).into(),
    ActorRef::downgrade(&actor_b).into(),
];

for handler in &weak_handlers {
    if let Some(strong) = handler.upgrade() {
        strong.tell(PingMsg).await?;
    }
    // else: actor was dropped, skip it
}
}

Available Methods

All handler traits provide these methods:

MethodTellHandlerAskHandler
tell(msg) / ask(msg)Async sendAsync send + reply
tell_with_timeout / ask_with_timeoutWith timeoutWith timeout
blocking_tell / blocking_askBlocking sendBlocking send + reply
clone_boxed()Clone handlerClone handler
downgrade()To weak handlerTo weak handler
as_control()Access lifecycleAccess lifecycle

Lifecycle Access via as_control()

Handler traits provide access to ActorControl for lifecycle management:

#![allow(unused)]
fn main() {
for handler in &handlers {
    let control = handler.as_control();
    println!("Actor {} alive: {}", control.identity(), control.is_alive());
}
}

Running the Example

cargo run --example handler_demo