#![cfg(feature="flume")] use trait_enumizer::flume_class; struct Qqq {} impl Qqq { fn foo(&self) -> String { dbg!("foo"); "qqq".to_owned() } async fn bar(&self, x: i32) -> i32 { dbg!("bar", x); 333 } async fn baz(&self, y: String, z: Vec) { dbg!("baz", y, z); } } // Begin of the part which is supposed to be auto-generated enum QqqEnum { Foo { ret: flume_class!(Sender)}, Bar { x: i32, ret: flume_class!(Sender) }, Baz { y: String, z: Vec }, } impl QqqEnum { async fn try_call(self, o: &Qqq) -> Result<(), flume_class!(SendError) > { match self { QqqEnum::Foo { ret} => flume_class!(send_async::(ret, o.foo())), QqqEnum::Bar { x , ret} => flume_class!(send_async::(ret, o.bar(x).await)), QqqEnum::Baz { y, z } => Ok(o.baz(y, z).await), } } } struct QqqUsualProxy Result<(), E>>(F); impl Result<(), E>> QqqUsualProxy { fn try_foo(&self) -> Result, E>{ let (tx, rx) = flume_class!(create::()); self.0(QqqEnum::Foo { ret: tx })?; Ok(flume_class!(recv::(rx))) } fn try_bar(&self, x: i32) -> Result, E> { let (tx, rx) = flume_class!(create::()); self.0(QqqEnum::Bar { x, ret: tx })?; Ok(flume_class!(recv::(rx))) } fn try_baz(&self, y: String, z: Vec) -> Result<(), E> { self.0(QqqEnum::Baz { y, z }) } } struct QqqAsyncProxy Fu, Fu: std::future::Future>>(F); impl Fu, Fu: std::future::Future>> QqqAsyncProxy { async fn try_foo(&self) -> Result, E> { let (tx, rx) = flume_class!(create::()); self.0(QqqEnum::Foo { ret: tx }).await?; Ok(flume_class!(recv_async::(rx))) } async fn try_bar(&self, x: i32) -> Result, E> { let (tx, rx) = flume_class!(create::()); self.0(QqqEnum::Bar { x, ret: tx }).await?; Ok(flume_class!(recv_async::(rx))) } async fn try_baz(&self, y: String, z: Vec) -> Result<(), E> { self.0(QqqEnum::Baz { y, z }).await } } // End of the part which is supposed to be auto-generated #[test] fn async_to_async() { let o = Qqq {}; let p = QqqAsyncProxy::(|c: QqqEnum| async { Ok(c.try_call(&o).await.unwrap()) }); dbg!(futures::executor::block_on(p.try_foo()).unwrap().unwrap()); dbg!(futures::executor::block_on(p.try_bar(4)).unwrap().unwrap()); dbg!(futures::executor::block_on(p.try_baz("qqq".to_owned(), vec![])).unwrap()); } #[test] fn usual_to_async() { let o = Qqq {}; let p = QqqUsualProxy::(|c: QqqEnum| Ok(futures::executor::block_on(c.try_call(&o)).unwrap())); dbg!(p.try_foo().unwrap().unwrap()); dbg!(p.try_bar(4).unwrap().unwrap()); dbg!(p.try_baz("qqq".to_owned(), vec![]).unwrap()); }