Handler

Trait Handler 

Source
pub trait Handler<M>: Actor
where M: Message,
{ // Required method fn handle<'life0, 'life1, 'async_trait>( &'life0 mut self, message: M, ctx: &'life1 mut Context<Self>, ) -> Pin<Box<dyn Future<Output = <M as Message>::Result> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait; }
Expand description

A trait indicating that an Actor can handle a given Message asynchronously, and the logic to handle the message.

This is an async_trait, so implementations should be annotated #[async_trait].

§Example

struct Msg;

impl Message for Msg {
    type Result = u32;
}

#[async_trait::async_trait]
impl Handler<Msg> for MyActor {
    async fn handle(&mut self, message: Msg, ctx: &mut Context<Self>) -> u32 {
        20
    }
}

fn main() {
    smol::block_on(async {
        let addr = MyActor.create(None).spawn(&mut Smol::Global);
        assert_eq!(addr.send(Msg).await, Ok(20));
    })
}

Required Methods§

Source

fn handle<'life0, 'life1, 'async_trait>( &'life0 mut self, message: M, ctx: &'life1 mut Context<Self>, ) -> Pin<Box<dyn Future<Output = <M as Message>::Result> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Handle a given message, returning its result.

This is an async_trait. See the trait documentation to see an example of how this method can be declared.

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§