2023-02-15 14:05:56 +04:00
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2023-03-07 15:18:55 +04:00
|
|
|
use std::sync::Arc;
|
2023-02-15 14:05:56 +04:00
|
|
|
|
|
|
|
use futures::Future;
|
|
|
|
use messagebus::{
|
2023-02-17 21:40:45 +04:00
|
|
|
bus::{Bus, MaskMatch},
|
2023-02-15 14:05:56 +04:00
|
|
|
cell::MsgCell,
|
2023-03-07 15:18:55 +04:00
|
|
|
derive_message_clone,
|
2023-02-15 14:05:56 +04:00
|
|
|
error::Error,
|
|
|
|
handler::Handler,
|
|
|
|
receivers::wrapper::HandlerWrapper,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Msg(pub u32);
|
2023-03-07 15:18:55 +04:00
|
|
|
derive_message_clone!(EXAMPLE_MSG, Msg, "example::Msg");
|
2023-02-15 14:05:56 +04:00
|
|
|
|
|
|
|
struct Test {
|
|
|
|
inner: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler<Msg> for Test {
|
|
|
|
type Response = Msg;
|
|
|
|
type HandleFuture<'a> = impl Future<Output = Result<Self::Response, Error>> + 'a;
|
2023-02-17 21:40:45 +04:00
|
|
|
type FlushFuture<'a> = impl Future<Output = Result<(), Error>> + 'a;
|
2023-03-07 15:18:55 +04:00
|
|
|
type CloseFuture<'a> = impl Future<Output = Result<(), Error>> + 'a;
|
2023-02-15 14:05:56 +04:00
|
|
|
|
2023-02-17 21:40:45 +04:00
|
|
|
fn handle(&self, msg: &mut MsgCell<Msg>, _bus: &Bus) -> Self::HandleFuture<'_> {
|
2023-02-28 14:51:32 +04:00
|
|
|
let msg = msg.get();
|
2023-02-15 14:05:56 +04:00
|
|
|
|
|
|
|
async move {
|
|
|
|
println!("msg {msg:?}");
|
|
|
|
let x = self.inner;
|
|
|
|
Ok(Msg(x + msg.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 21:40:45 +04:00
|
|
|
fn flush(&mut self, _bus: &Bus) -> Self::FlushFuture<'_> {
|
|
|
|
async move { Ok(()) }
|
2023-02-15 14:05:56 +04:00
|
|
|
}
|
2023-03-07 15:18:55 +04:00
|
|
|
|
|
|
|
fn close(&mut self) -> Self::CloseFuture<'_> {
|
|
|
|
async move { Ok(()) }
|
|
|
|
}
|
2023-02-15 14:05:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run() -> Result<(), Error> {
|
|
|
|
let bus = Bus::new();
|
|
|
|
|
2023-02-17 21:40:45 +04:00
|
|
|
let wrapper = HandlerWrapper::new(Arc::new(Test { inner: 12 }));
|
|
|
|
bus.register(wrapper, MaskMatch::all());
|
2023-02-15 14:05:56 +04:00
|
|
|
|
2023-03-16 18:27:31 +04:00
|
|
|
let res: Msg = bus.request(Msg(13)).await?.result().await?;
|
2023-02-15 14:05:56 +04:00
|
|
|
println!("request result got {:?}", res);
|
|
|
|
|
2023-02-17 21:40:45 +04:00
|
|
|
bus.send(Msg(12)).await?;
|
|
|
|
|
|
|
|
bus.close().await;
|
|
|
|
bus.wait().await;
|
|
|
|
|
2023-02-15 14:05:56 +04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
run().await.unwrap();
|
|
|
|
}
|