Trait appliance::Handler [−][src]
A trait which must be implemented for all appliances which are intended to receive
messages of type M
. One appliance can handle multiple message types.
Handler’s logic is strongly encouraged to include only fast (non-blocking) and synchronous mutations of the appliance state. Otherwise, the appliance’s event loop may get slow, and hence flood the internal buffer causing message sending denials.
Example
type Counter = Appliance<'static, usize>; struct Ping; impl Message for Ping { type Result = usize; } impl Handler<Ping> for Counter { fn handle(&mut self, _msg: &Ping) -> usize { *self.state() += 1; *self.state() } } struct Reset; impl Message for Reset { type Result = (); } impl Handler<Reset> for Counter { fn handle(&mut self, _msg: &Reset) { *self.state() = 0; } } const BUF_SIZE: usize = 10; fn main() -> Result<(), appliance::Error> { let descriptor = Appliance::new_bounded(&DEFAULT_EXECUTOR, 0, BUF_SIZE); assert_eq!(*descriptor.send_and_wait_sync(Ping)?, 1); assert_eq!(*descriptor.send_and_wait_sync(Ping)?, 2); descriptor.send(Reset)?; assert_eq!(*descriptor.send_and_wait_sync(Ping)?, 1); Ok(()) }