Trait Handler

Source
pub trait Handler<IN>: Sized + Actor {
    type Result;

    // Required method
    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 Self: 'async_trait,
             'life0: 'async_trait,
             'life1: '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);
}

Required Associated Types§

Source

type Result

Result of the message processing.

Required Methods§

Source

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 Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Processes a message.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§