macro_rules! impl_message_handler {
($actor_type:ty, [$($msg_type:ty),* $(,)?]) => { ... };
}Expand description
Implements the MessageHandler trait for a given actor type.
This macro simplifies the process of handling multiple message types within an actor.
It generates the necessary boilerplate code to downcast a Box<dyn Any + Send>
message to its concrete type and then calls the appropriate Message::handle
implementation on the actor.
§Usage
ⓘ
struct MyActor;
impl Actor for MyActor { /* ... */ }
struct Msg1;
struct Msg2;
impl Message<Msg1> for MyActor {
type Reply = ();
async fn handle(&mut self, msg: Msg1) -> Self::Reply { /* ... */ }
}
impl Message<Msg2> for MyActor {
type Reply = String;
async fn handle(&mut self, msg: Msg2) -> Self::Reply { /* ... */ "response".to_string() }
}
// This will implement `MessageHandler` for `MyActor`, allowing it to handle `Msg1` and `Msg2`.
impl_message_handler!(MyActor, [Msg1, Msg2]);§Arguments
$actor_type: The type of the actor for which to implementMessageHandler.[$($msg_type:ty),+]: A list of message types that the actor can handle.
§Internals
This macro facilitates dynamic message dispatch by downcasting Box<dyn std::any::Any + Send>
message payloads to their concrete types at runtime.