messagebus/examples/demo_slow.rs

65 lines
1.5 KiB
Rust
Raw Normal View History

use messagebus::{receivers, Bus, Handler};
2020-12-17 17:35:11 +04:00
struct TmpReceiver;
impl Handler<f32> for TmpReceiver {
type Error = anyhow::Error;
type Response = ();
fn handle(&self, msg: f32, _bus: &Bus) -> Result<Self::Response, Self::Error> {
2020-12-17 17:35:11 +04:00
println!("---> f32 {}", msg);
std::thread::sleep(std::time::Duration::from_secs(5));
println!("done");
Ok(())
}
}
impl Handler<u16> for TmpReceiver {
type Error = anyhow::Error;
type Response = ();
fn handle(&self, msg: u16, _bus: &Bus) -> Result<Self::Response, Self::Error> {
2020-12-17 17:35:11 +04:00
println!("---> u16 {}", msg);
Ok(())
}
}
impl Handler<u32> for TmpReceiver {
type Error = anyhow::Error;
type Response = ();
fn handle(&self, msg: u32, _bus: &Bus) -> Result<Self::Response, Self::Error> {
2020-12-17 17:35:11 +04:00
println!("---> u32 {}", msg);
Ok(())
}
}
#[tokio::main]
async fn main() {
let (b, poller) = Bus::build()
.register(TmpReceiver)
.subscribe::<f32, receivers::BufferUnorderedSync<_>, _, _>(8, Default::default())
.subscribe::<u16, receivers::BufferUnorderedSync<_>, _, _>(8, Default::default())
.subscribe::<u32, receivers::BufferUnorderedSync<_>, _, _>(8, Default::default())
2020-12-17 17:35:11 +04:00
.done()
.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;
2021-06-23 16:20:10 +04:00
println!("closed");
poller.await;
println!("[done]");
2020-12-17 17:35:11 +04:00
}