Trait xtra::Handler

source ·
pub trait Handler<M>: Actor {
    type Return: Send + 'static;

    // Required method
    fn handle(
        &mut self,
        message: M,
        ctx: &mut Context<Self>
    ) -> impl Future<Output = Self::Return> + Send;
}
Expand description

Defines that an Actor can handle a given message M.

§Example

struct Msg;


impl Handler<Msg> for MyActor {
    type Return = u32;

    async fn handle(&mut self, message: Msg, ctx: &mut Context<Self>) -> u32 {
        20
    }
}

fn main() {
    smol::block_on(async {
        let addr = xtra::spawn_smol(MyActor, Mailbox::unbounded());
        assert_eq!(addr.send(Msg).await, Ok(20));
    })
}

Required Associated Types§

source

type Return: Send + 'static

The return value of this handler.

Required Methods§

source

fn handle( &mut self, message: M, ctx: &mut Context<Self> ) -> impl Future<Output = Self::Return> + Send

Handle a given message, returning its result.

Object Safety§

This trait is not object safe.

Implementors§