Compare commits

...

1 Commits

Author SHA1 Message Date
b63a3f9f20 proof of concept: it's just work with GATs 2021-09-16 13:41:28 +04:00
3 changed files with 107 additions and 53 deletions

View File

@ -1,4 +1,6 @@
use async_trait::async_trait; #![feature(associated_type_defaults, generic_associated_types, type_alias_impl_trait)]
use futures::Future;
use messagebus::{derive::Message, error, AsyncHandler, Bus, Handler, Message, TypeTagged}; use messagebus::{derive::Message, error, AsyncHandler, Bus, Handler, Message, TypeTagged};
use thiserror::Error; use thiserror::Error;
@ -37,115 +39,164 @@ struct MsgI32(i32);
#[message(clone)] #[message(clone)]
struct MsgI16(i16); struct MsgI16(i16);
#[async_trait]
impl AsyncHandler<MsgF32> for TmpReceiver { impl AsyncHandler<MsgF32> for TmpReceiver {
type Error = Error; type Error = Error;
type Response = (); type Response = ();
async fn handle(&self, msg: MsgF32, bus: &Bus) -> Result<Self::Response, Self::Error> { type AsyncHandleFuture<'a> = impl Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'a;
bus.send(MsgU16(1)).await?; type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
println!("TmpReceiver ---> {:?} {}", msg, msg.type_tag()); fn handle(&self, msg: MsgF32, bus: &Bus) -> Self::AsyncHandleFuture<'_> {
let bus = bus.clone();
Ok(()) async move {
bus.send(MsgU16(1)).await?;
println!("TmpReceiver ---> {:?} {}", msg, msg.type_tag());
Ok(())
}
} }
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> { fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_> {
println!("TmpReceiver f32: sync"); async move {
println!("TmpReceiver f32: sync");
Ok(()) Ok(())
}
} }
} }
#[async_trait]
impl AsyncHandler<MsgU16> for TmpReceiver { impl AsyncHandler<MsgU16> for TmpReceiver {
type Error = Error; type Error = Error;
type Response = (); type Response = ();
async fn handle(&self, msg: MsgU16, bus: &Bus) -> Result<Self::Response, Self::Error> { type AsyncHandleFuture<'a> = impl Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'a;
bus.send(MsgU32(2)).await?; type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
println!("TmpReceiver ---> {:?}", msg);
Ok(()) fn handle(&self, msg: MsgU16, bus: &Bus) -> Self::AsyncHandleFuture<'_> {
let bus = bus.clone();
async move {
bus.send(MsgU32(2)).await?;
println!("TmpReceiver ---> {:?}", msg);
Ok(())
}
} }
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> { fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_> {
println!("TmpReceiver u16: sync"); async move {
println!("TmpReceiver u16: sync");
Ok(()) Ok(())
}
} }
} }
#[async_trait]
impl AsyncHandler<MsgU32> for TmpReceiver { impl AsyncHandler<MsgU32> for TmpReceiver {
type Error = Error; type Error = Error;
type Response = (); type Response = ();
async fn handle(&self, msg: MsgU32, bus: &Bus) -> Result<Self::Response, Self::Error> { type AsyncHandleFuture<'a> = impl Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'a;
bus.send(MsgI32(3)).await?; type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
println!("TmpReceiver ---> {:?}", msg);
Ok(()) fn handle(&self, msg: MsgU32, bus: &Bus) -> Self::AsyncHandleFuture<'_> {
let bus = bus.clone();
async move {
bus.send(MsgI32(3)).await?;
println!("TmpReceiver ---> {:?}", msg);
Ok(())
}
} }
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> {
println!("TmpReceiver u32: sync");
Ok(()) fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_> {
async move {
println!("TmpReceiver u32: sync");
Ok(())
}
} }
} }
#[async_trait]
impl AsyncHandler<MsgI32> for TmpReceiver { impl AsyncHandler<MsgI32> for TmpReceiver {
type Error = Error; type Error = Error;
type Response = (); type Response = ();
async fn handle(&self, msg: MsgI32, bus: &Bus) -> Result<Self::Response, Self::Error> { type AsyncHandleFuture<'a> = impl Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'a;
bus.send(MsgI16(4)).await?; type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
println!("TmpReceiver ---> {:?}", msg);
Ok(()) fn handle(&self, msg: MsgI32, bus: &Bus) -> Self::AsyncHandleFuture<'_> {
let bus = bus.clone();
async move {
bus.send(MsgI16(4)).await?;
println!("TmpReceiver ---> {:?}", msg);
Ok(())
}
} }
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> { fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_> {
println!("TmpReceiver i32: sync"); async move {
println!("TmpReceiver i32: sync");
Ok(()) Ok(())
}
} }
} }
#[async_trait]
impl AsyncHandler<MsgI16> for TmpReceiver { impl AsyncHandler<MsgI16> for TmpReceiver {
type Error = Error; type Error = Error;
type Response = (); type Response = ();
async fn handle(&self, msg: MsgI16, _bus: &Bus) -> Result<Self::Response, Self::Error> { type AsyncHandleFuture<'a> = impl Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'a;
println!("TmpReceiver ---> {:?}", msg); type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
Ok(()) fn handle(&self, msg: MsgI16, _bus: &Bus) -> Self::AsyncHandleFuture<'_> {
async move {
println!("TmpReceiver ---> {:?}", msg);
Ok(())
}
} }
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> {
println!("TmpReceiver i16: sync");
Ok(()) fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_> {
async move {
println!("TmpReceiver i16: sync");
Ok(())
}
} }
} }
#[async_trait]
impl AsyncHandler<MsgI32> for TmpReceiver2 { impl AsyncHandler<MsgI32> for TmpReceiver2 {
type Error = Error; type Error = Error;
type Response = (); type Response = ();
async fn handle(&self, msg: MsgI32, bus: &Bus) -> Result<Self::Response, Self::Error> { type AsyncHandleFuture<'a> = impl Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'a;
println!("TmpReceiver2: ---> {:?}", msg); type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
bus.send(MsgI16(5)).await?; fn handle(&self, msg: MsgI32, bus: &Bus) -> Self::AsyncHandleFuture<'_> {
let bus = bus.clone();
Ok(()) async move {
println!("TmpReceiver2: ---> {:?}", msg);
bus.send(MsgI16(5)).await?;
Ok(())
}
} }
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> { fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_> {
println!("TmpReceiver2: i32: sync"); async move {
println!("TmpReceiver2: i32: sync");
Ok(()) Ok(())
}
} }
} }

View File

@ -2,6 +2,7 @@ use core::iter::FromIterator;
use crate::{error::StdSyncSendError, Bus, Message}; use crate::{error::StdSyncSendError, Bus, Message};
use async_trait::async_trait; use async_trait::async_trait;
use futures::Future;
pub trait Handler<M: Message>: Send + Sync { pub trait Handler<M: Message>: Send + Sync {
type Error: StdSyncSendError; type Error: StdSyncSendError;
@ -13,15 +14,15 @@ pub trait Handler<M: Message>: Send + Sync {
} }
} }
#[async_trait]
pub trait AsyncHandler<M: Message>: Send + Sync { pub trait AsyncHandler<M: Message>: Send + Sync {
type Error: StdSyncSendError; type Error: StdSyncSendError;
type Response: Message; type Response: Message;
async fn handle(&self, msg: M, bus: &Bus) -> Result<Self::Response, Self::Error>; type AsyncHandleFuture<'a>: Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'a;
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> { type AsyncSyncFuture<'a>: Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
Ok(())
} fn handle(&self, msg: M, bus: &Bus) -> Self::AsyncHandleFuture<'_>;
fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_>;
} }
pub trait SynchronizedHandler<M: Message>: Send { pub trait SynchronizedHandler<M: Message>: Send {

View File

@ -1,3 +1,5 @@
#![feature(associated_type_defaults, generic_associated_types, type_alias_impl_trait)]
mod builder; mod builder;
mod envelop; mod envelop;
pub mod error; pub mod error;