use anyhow::{anyhow, Result}; use async_trait::async_trait; use tokio::{ stream::StreamExt, sync::mpsc::{Receiver, Sender}, }; use xmr_btc::{alice, bob, transport::SendReceive}; #[derive(Debug)] pub struct Transport { pub sender: Sender, pub receiver: Receiver, } #[async_trait] impl SendReceive for Transport { async fn send_message(&mut self, message: alice::Message) -> Result<()> { let _ = self .sender .send(message) .await .map_err(|_| anyhow!("failed to send message"))?; Ok(()) } async fn receive_message(&mut self) -> Result { let message = self .receiver .next() .await .ok_or_else(|| anyhow!("failed to receive message"))?; Ok(message) } } #[async_trait] impl SendReceive for Transport { async fn send_message(&mut self, message: bob::Message) -> Result<()> { let _ = self .sender .send(message) .await .map_err(|_| anyhow!("failed to send message"))?; Ok(()) } async fn receive_message(&mut self) -> Result { let message = self .receiver .next() .await .ok_or_else(|| anyhow!("failed to receive message"))?; Ok(message) } }