pub trait Handler<IN>: Sized + Actor {
type Result;
fn handle<'life0, 'life1, 'async_trait>(
&'life0 mut self,
input: IN,
context: &'life1 Context<Self>
) -> Pin<Box<dyn Future<Output = Self::Result> + Send + 'async_trait>>
where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
}
Expand description
Handler
is an extension trait for Actor
that enables it
to process messages and return results of the message processing.
Note: Handler workflow guarantees that sent messages will be delivered in order.
Examples
This example assumes that messages
is used with rt-tokio
feature enabled.
struct Sum;
#[async_trait]
impl Actor for Sum {}
#[async_trait]
impl Handler<(u8, u8)> for Sum {
type Result = u16;
async fn handle(&mut self, (a, b): (u8, u8), context: &Context<Self>) -> u16 {
(a as u16) + (b as u16)
}
}
#[tokio::main]
async fn main() {
let mut addr = Sum.spawn();
let result = addr.send((22, 20)).await.unwrap();
assert_eq!(result, 42);
}