meio_extra/
custom_action.rs

1//! Contains the custom type of action for actors.
2
3use anyhow::Error;
4use async_trait::async_trait;
5use meio::handlers::{Handler, Priority};
6use meio::prelude::{Actor, Context};
7
8// TODO: Add `custom_act` method the the `Address`.
9
10/// The fully own type of messages.
11pub trait CustomAction: Send + 'static {}
12
13/// The trait for actors for handling incoming `CustomAction` instances.
14#[async_trait]
15pub trait CustomActionHandler<I: CustomAction>: Actor {
16    /// Handles the incoming message.
17    async fn handle(&mut self, input: I, _ctx: &mut Context<Self>) -> Result<(), Error>;
18}
19
20/// The envelope for handler's message.
21///
22/// It wraps a `CustomAction` and implements a `Handler` to
23/// `take` the value from the inner `Option` and call the
24/// specific handler trait.
25struct CustomActionHandlerImpl<I> {
26    input: Option<I>,
27}
28
29/// Implementation of a `Handler` for `CustomActionHandlerImpl` wrapper.
30#[async_trait]
31impl<A, I> Handler<A> for CustomActionHandlerImpl<I>
32where
33    A: CustomActionHandler<I>,
34    I: CustomAction,
35{
36    fn priority(&self) -> Priority {
37        Priority::Normal
38    }
39
40    async fn handle(&mut self, actor: &mut A, ctx: &mut Context<A>) -> Result<(), Error> {
41        let input = self.input.take().expect("action handler called twice");
42        actor.handle(input, ctx).await
43    }
44}