#[macro_export]
macro_rules! setup_linkk {
($v:vis, $struct1:ident<$t:ty>, $struct2:ident<$t2:ty>) => {
$v struct $struct1 {
$v tx: std::sync::mpsc::Sender<$t>,
$v rx: std::sync::mpsc::Receiver<$t2>,
}
$v struct $struct2 {
$v tx: std::sync::mpsc::Sender<$t2>,
$v rx: std::sync::mpsc::Receiver<$t>,
}
impl $struct1 {
$v fn send(&self, t: $t) -> std::result::Result<(), std::sync::mpsc::SendError<$t>> {
self.tx.send(t)
}
$v fn recv(&self) -> Result<$t2, std::sync::mpsc::RecvError> {
self.rx.recv()
}
pub fn new(tx: std::sync::mpsc::Sender<$t>, rx: std::sync::mpsc::Receiver<$t2>)-> Self {
Self {
tx,
rx
}
}
}
impl $struct2 {
$v fn send(&self, t: $t2) -> std::result::Result<(), std::sync::mpsc::SendError<$t2>> {
self.tx.send(t)
}
$v fn recv(&self) -> Result<$t, std::sync::mpsc::RecvError> {
self.rx.recv()
}
pub fn new(tx: std::sync::mpsc::Sender<$t2>, rx: std::sync::mpsc::Receiver<$t>)-> Self {
Self {
tx,
rx
}
}
}
pub fn make_new_linkk() -> ($struct1, $struct2) {
let (tx1, rx1) = std::sync::mpsc::channel::<$t>();
let (tx2, rx2) = std::sync::mpsc::channel::<$t2>();
($struct1 { tx: tx1, rx: rx2 }, $struct2 { tx: tx2, rx: rx1 })
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_link_macro() {
setup_linkk!(pub, Window2os<u32>, Os2Window<u64>);
let (link2, link1) = make_new_linkk();
link2.send(42).unwrap();
assert_eq!(link1.recv().unwrap(), 42u32);
link1.tx.send(43 as u64).unwrap();
assert_eq!(link2.rx.recv().unwrap(), 43);
}
}