proof of concept: it's just work with GATs

This commit is contained in:
Andrey Tkachenko 2021-09-16 13:41:28 +04:00
parent ac12bb08ab
commit b63a3f9f20
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 thiserror::Error;
@ -37,117 +39,166 @@ struct MsgI32(i32);
#[message(clone)]
struct MsgI16(i16);
#[async_trait]
impl AsyncHandler<MsgF32> for TmpReceiver {
type Error = Error;
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;
type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
fn handle(&self, msg: MsgF32, bus: &Bus) -> Self::AsyncHandleFuture<'_> {
let bus = bus.clone();
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<'_> {
async move {
println!("TmpReceiver f32: sync");
Ok(())
}
}
}
#[async_trait]
impl AsyncHandler<MsgU16> for TmpReceiver {
type Error = Error;
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;
type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
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<'_> {
async move {
println!("TmpReceiver u16: sync");
Ok(())
}
}
}
#[async_trait]
impl AsyncHandler<MsgU32> for TmpReceiver {
type Error = Error;
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;
type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
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> {
}
fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_> {
async move {
println!("TmpReceiver u32: sync");
Ok(())
}
}
}
#[async_trait]
impl AsyncHandler<MsgI32> for TmpReceiver {
type Error = Error;
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;
type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
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<'_> {
async move {
println!("TmpReceiver i32: sync");
Ok(())
}
}
}
#[async_trait]
impl AsyncHandler<MsgI16> for TmpReceiver {
type Error = Error;
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;
type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
fn handle(&self, msg: MsgI16, _bus: &Bus) -> Self::AsyncHandleFuture<'_> {
async move {
println!("TmpReceiver ---> {:?}", msg);
Ok(())
}
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> {
}
fn sync(&self, _bus: &Bus) -> Self::AsyncSyncFuture<'_> {
async move {
println!("TmpReceiver i16: sync");
Ok(())
}
}
}
#[async_trait]
impl AsyncHandler<MsgI32> for TmpReceiver2 {
type Error = Error;
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;
type AsyncSyncFuture<'a> = impl Future<Output = Result<(), Self::Error>> + Send + Sync + 'a;
fn handle(&self, msg: MsgI32, bus: &Bus) -> Self::AsyncHandleFuture<'_> {
let bus = bus.clone();
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<'_> {
async move {
println!("TmpReceiver2: i32: sync");
Ok(())
}
}
}
impl Handler<MsgI16> for TmpReceiver2 {
type Error = Error;

View File

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