atomic_actor/handler.rs
1use async_trait::async_trait;
2
3use crate::{Actor, Context};
4
5/// [`Handler`] handles a certain type of message asyncronously.
6///
7/// # Example
8///
9/// ```rust
10/// use async_trait::async_trait;
11/// use atomic_actor::*;
12///
13/// struct AddOne;
14///
15/// impl Actor for AddOne {
16/// type Context = Context<Self>;
17/// }
18///
19/// #[async_trait]
20/// impl Handler<i32> for AddOne {
21/// type Result = i32;
22///
23/// async fn handle(&mut self, message: i32, _: &mut Context<Self>) -> i32 {
24/// message + 1
25/// }
26/// }
27///
28/// #[tokio::main]
29/// async fn main() {
30/// let addr = AddOne.start();
31/// assert_eq!(addr.send(1).unwrap().await, 2);
32/// }
33#[async_trait]
34pub trait Handler<M: 'static + Send>: Actor {
35 /// The result of the handler. Must be [`Send`].
36 type Result: Send;
37
38 /// Handles the message.
39 async fn handle(&mut self, message: M, context: &mut Context<Self>) -> Self::Result;
40}