messagebus/examples/demo_slow.rs

81 lines
1.7 KiB
Rust

use messagebus::{error, Bus, Handler, Message, Module};
use thiserror::Error;
#[derive(Debug, Error)]
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())
}
}
struct TmpReceiver;
impl Handler<f32> for TmpReceiver {
type Error = Error;
type Response = ();
fn handle(&self, msg: f32, _bus: &Bus) -> Result<Self::Response, Self::Error> {
println!("---> f32 {}", msg);
std::thread::sleep(std::time::Duration::from_secs(5));
println!("done");
Ok(())
}
}
impl Handler<u16> for TmpReceiver {
type Error = Error;
type Response = ();
fn handle(&self, msg: u16, _bus: &Bus) -> Result<Self::Response, Self::Error> {
println!("---> u16 {}", msg);
Ok(())
}
}
impl Handler<u32> for TmpReceiver {
type Error = Error;
type Response = ();
fn handle(&self, msg: u32, _bus: &Bus) -> Result<Self::Response, Self::Error> {
println!("---> u32 {}", msg);
Ok(())
}
}
fn module() -> Module {
Module::new()
.register(TmpReceiver)
.subscribe_sync::<f32>(8, Default::default())
.subscribe_sync::<u16>(8, Default::default())
.subscribe_sync::<u32>(8, Default::default())
.done()
}
#[tokio::main]
async fn main() {
let (b, poller) = Bus::build().add_module(module()).build();
b.send(32f32).await.unwrap();
b.send(11u16).await.unwrap();
b.send(32u32).await.unwrap();
println!("flush");
b.flush().await;
println!("close");
b.close().await;
println!("closed");
poller.await;
println!("[done]");
}