messagebus/examples/demo_slow.rs

93 lines
2.0 KiB
Rust
Raw Normal View History

use messagebus::{
derive::{Error as MbError, Message},
error, Bus, Handler, Message, Module,
};
use thiserror::Error;
#[derive(Debug, Error, MbError)]
enum Error {
#[error("Error({0})")]
Error(anyhow::Error),
}
impl<M: Message> From<error::Error<M>> for Error {
fn from(err: error::Error<M>) -> Self {
Self::Error(err.into())
}
}
2020-12-17 17:35:11 +04:00
#[derive(Debug, Clone, Message)]
struct MsgF32(pub f32);
#[derive(Debug, Clone, Message)]
struct MsgU32(pub u32);
#[derive(Debug, Clone, Message)]
struct MsgU16(pub u16);
2020-12-17 17:35:11 +04:00
struct TmpReceiver;
impl Handler<MsgF32> for TmpReceiver {
2021-06-24 13:32:19 +04:00
type Error = Error;
type Response = ();
fn handle(&self, msg: MsgF32, _bus: &Bus) -> Result<Self::Response, Self::Error> {
println!("---> f32 {:?}", msg);
2020-12-17 17:35:11 +04:00
std::thread::sleep(std::time::Duration::from_secs(5));
println!("done");
Ok(())
}
}
impl Handler<MsgU16> for TmpReceiver {
2021-06-24 13:32:19 +04:00
type Error = Error;
type Response = ();
fn handle(&self, msg: MsgU16, _bus: &Bus) -> Result<Self::Response, Self::Error> {
println!("---> u16 {:?}", msg);
2020-12-17 17:35:11 +04:00
Ok(())
}
}
impl Handler<MsgU32> for TmpReceiver {
2021-06-24 13:32:19 +04:00
type Error = Error;
type Response = ();
fn handle(&self, msg: MsgU32, _bus: &Bus) -> Result<Self::Response, Self::Error> {
println!("---> u32 {:?}", msg);
2020-12-17 17:35:11 +04:00
Ok(())
}
}
2021-06-28 19:37:53 +04:00
fn module() -> Module {
Module::new()
.register(TmpReceiver)
.subscribe_sync::<MsgF32>(8, Default::default())
.subscribe_sync::<MsgU16>(8, Default::default())
.subscribe_sync::<MsgU32>(8, Default::default())
.done()
2021-06-28 19:37:53 +04:00
}
#[tokio::main]
async fn main() {
let (b, poller) = Bus::build().add_module(module()).build();
2020-12-17 17:35:11 +04:00
b.send(MsgF32(32f32)).await.unwrap();
b.send(MsgU16(11u16)).await.unwrap();
b.send(MsgU32(32u32)).await.unwrap();
2020-12-17 17:35:11 +04:00
println!("flush");
b.flush_all().await;
println!("close");
b.close().await;
2021-06-23 16:20:10 +04:00
println!("closed");
poller.await;
println!("[done]");
2020-12-17 17:35:11 +04:00
}