1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! The extensions for the `CustomAction`.
//!
//! Like `Consumer` implements `ActionHandler` trait
//! in the standard `meio` handlers.

use super::custom_action::*;
use anyhow::Error;
use async_trait::async_trait;
use meio::prelude::{Actor, Context};

/// The handler for special types of actions.
#[async_trait]
trait ExtendedHandler: Actor {
    /// Handle the action in this method.
    async fn handle(&mut self, value: u8, ctx: &mut Context<Self>) -> Result<(), Error>;
    /// Other method for special cases.
    async fn done(&mut self, ctx: &mut Context<Self>) -> Result<(), Error>;
}

/// The example of customized action with own inner value.
struct ExtendedAction {
    inner_value: Option<u8>,
}

/// Implement `CustomAction` for it.
///
/// Implementing `Action` is meaningless, because it's impossible
/// to implement `ActionHandler` in the third-party crate (here).
impl CustomAction for ExtendedAction {}

#[async_trait]
impl<T: Actor> CustomActionHandler<ExtendedAction> for T
where
    T: ExtendedHandler,
{
    async fn handle(
        &mut self,
        input: ExtendedAction,
        ctx: &mut Context<Self>,
    ) -> Result<(), Error> {
        if let Some(value) = input.inner_value {
            // Call the `ExtendedHandler` with the provided value
            ExtendedHandler::handle(self, value, ctx).await
        } else {
            // No more messages expected, or empty value, or other signal.
            ExtendedHandler::done(self, ctx).await
        }
    }
}