[][src]Trait xtra::SyncHandler

pub trait SyncHandler<M: Message>: Actor {
    fn handle(&mut self, message: M, ctx: &mut Context<Self>) -> M::Result;
}

A trait indicating that an Actor can handle a given Message synchronously, and the logic to handle the message. A SyncHandler implementation automatically creates a corresponding Handler impl. This, however, is not just sugar over the asynchronous Handler trait -- it is also slightly faster than it for handling due to how they get specialized under the hood.

Example

struct Msg;

impl Message for Msg {
    type Result = u32;
}

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

#[smol_potat::main]
async fn main() {
    let addr = MyActor.spawn();
    assert_eq!(addr.send(Msg).await, Ok(20));
}

Required methods

fn handle(&mut self, message: M, ctx: &mut Context<Self>) -> M::Result

Handle a given message, returning its result.

Loading content...

Implementors

Loading content...