Message Handlers Macro
The #[message_handlers] attribute macro combined with #[handler] method attributes is the recommended approach for defining message handlers in rsActor. It automatically generates the necessary Message trait implementations from annotated methods.
Purpose
When an actor needs to handle multiple message types, manually implementing the Message trait for each one is verbose. The #[message_handlers] macro automates this by generating all the boilerplate from simple method signatures.
Basic Example
#![allow(unused)]
fn main() {
use rsactor::{Actor, ActorRef, message_handlers, spawn};
#[derive(Actor)]
struct CounterActor {
value: i32,
}
// Define message types
struct Add(i32);
struct GetValue;
// Use message_handlers macro with handler attributes
#[message_handlers]
impl CounterActor {
#[handler]
async fn handle_add(&mut self, msg: Add, _: &ActorRef<Self>) -> i32 {
self.value += msg.0;
self.value
}
#[handler]
async fn handle_get(&mut self, _: GetValue, _: &ActorRef<Self>) -> i32 {
self.value
}
// Regular methods can coexist without the #[handler] attribute
fn reset(&mut self) {
self.value = 0;
}
}
}
How It Works
Each #[handler] method is transformed into a Message<T> trait implementation:
- The first parameter after
&mut selfdetermines the message typeT - The return type becomes
Message<T>::Reply - The method body becomes the
handle()implementation
The macro generates the equivalent of:
#![allow(unused)]
fn main() {
impl Message<Add> for CounterActor {
type Reply = i32;
async fn handle(&mut self, msg: Add, actor_ref: &ActorRef<Self>) -> Self::Reply {
self.value += msg.0;
self.value
}
}
}
Generic Actor Support
The macro fully supports generic actors:
#![allow(unused)]
fn main() {
use rsactor::{Actor, ActorRef, message_handlers, spawn};
use std::fmt::Debug;
#[derive(Debug)]
struct GenericActor<T: Send + Debug + Clone + 'static> {
value: Option<T>,
}
impl<T: Send + Debug + Clone + 'static> Actor for GenericActor<T> {
type Error = String;
type Args = ();
async fn on_start(_: Self::Args, _: &ActorRef<Self>) -> Result<Self, Self::Error> {
Ok(GenericActor { value: None })
}
}
struct SetValue<T: Send + Debug + 'static>(T);
struct GetValue;
#[message_handlers]
impl<T: Send + Debug + Clone + 'static> GenericActor<T> {
#[handler]
async fn handle_set(&mut self, msg: SetValue<T>, _: &ActorRef<Self>) -> () {
self.value = Some(msg.0);
}
#[handler]
async fn handle_get(&mut self, _: GetValue, _: &ActorRef<Self>) -> Option<T> {
self.value.clone()
}
}
}
Error Handling with on_tell_result
When a handler returns Result<T, E>, the macro automatically generates an on_tell_result implementation that logs errors for tell() calls:
#![allow(unused)]
fn main() {
#[message_handlers]
impl MyActor {
// Returning Result<T, E> auto-generates error logging for tell() calls
#[handler]
async fn handle_process(&mut self, msg: ProcessData, _: &ActorRef<Self>) -> Result<(), MyError> {
self.process(msg.data)?;
Ok(())
}
// Use #[handler(no_log)] to suppress automatic error logging
#[handler(no_log)]
async fn handle_silent(&mut self, msg: SilentOp, _: &ActorRef<Self>) -> Result<(), MyError> {
// Errors won't be automatically logged for tell() calls
Ok(())
}
}
}
#[derive(Actor)] Macro
For simple actors that don’t need custom initialization logic, use #[derive(Actor)]:
#![allow(unused)]
fn main() {
#[derive(Actor)]
struct SimpleActor {
name: String,
count: u32,
}
// spawn takes the struct instance directly as Args
let actor = SimpleActor { name: "test".into(), count: 0 };
let (actor_ref, handle) = spawn::<SimpleActor>(actor);
}
This generates an Actor impl where:
type Args = Self(the struct itself)type Error = std::convert::Infallible(never fails)on_startsimply returns the provided instance
Benefits
- Selective Processing: Only methods with
#[handler]become message handlers - Clean Separation: Regular methods coexist with handlers in the same
implblock - Automatic
on_tell_result: Error logging forResultreturn types (suppressed via#[handler(no_log)]) - Generic Support: Full support for generic actors and message types
- Compile-Time Safety: Message handler signatures are verified at compile time
- Reduced Boilerplate: No manual
Messagetrait implementations needed
Running the Example
cargo run --example derive_macro_demo
cargo run --example unified_macro_demo