#![feature(type_alias_impl_trait)] use std::{alloc::Layout, any::Any, sync::Arc}; use futures::Future; use messagebus_new::{ cell::MsgCell, error::Error, handler::Handler, message::{Message, SharedMessage}, receiver::Receiver, receivers::wrapper::HandlerWrapper, type_tag::{TypeTag, TypeTagInfo}, }; #[derive(Debug, Clone)] struct Msg(pub u32); impl Message for Msg { fn TYPE_TAG() -> TypeTag where Self: Sized, { TypeTagInfo::parse("demo::Msg").unwrap().into() } fn type_tag(&self) -> TypeTag { Msg::TYPE_TAG() } fn type_layout(&self) -> Layout { Layout::for_value(self) } fn as_any_ref(&self) -> &dyn Any { self } fn as_any_mut(&mut self) -> &mut dyn Any { self } fn as_any_boxed(self: Box) -> Box { self as _ } fn as_any_arc(self: Arc) -> Arc { self as _ } fn as_shared_ref(&self) -> Option<&dyn SharedMessage> { None } fn as_shared_mut(&mut self) -> Option<&mut dyn SharedMessage> { None } fn as_shared_boxed(self: Box) -> Result, Box> { Err(self) } fn as_shared_arc(self: Arc) -> Option> { None } fn try_clone_into(&self, into: &mut dyn Message) -> bool { false } fn try_clone_boxed(&self) -> Option> { None } fn try_clone(&self) -> Option where Self: Sized, { Some(Self(self.0)) } } struct Test { inner: u32, } impl<'a> Handler<'a, Msg> for Test { type Response = Msg; type HandleFuture = impl Future> + 'a; fn handle(&'a self, msg: Msg) -> Self::HandleFuture { async move { println!("msg {msg:?}"); let x = self.inner; Ok(Msg(x + msg.0)) } } } async fn run() -> Result<(), Error> { let wrapper = HandlerWrapper::new(Arc::new(Test { inner: 12 })); let receiver = wrapper.into_abstract_arc(); receiver.send(Msg(12)).await?; println!("sent"); let res: Msg = receiver.result(()).await?; println!("send result got {:?}", res); let res: Msg = receiver.request(Msg(13)).await?; println!("request result got {:?}", res); Ok(()) } #[tokio::main] async fn main() { run().await.unwrap(); }