Implement transport traits with generics

This commit is contained in:
rishflab 2020-10-09 09:46:24 +11:00
parent bf576bf251
commit 666c121bb3
4 changed files with 19 additions and 38 deletions

View file

@ -3,7 +3,7 @@ use crate::{
bitcoin::{BroadcastSignedTransaction, WatchForRawTransaction}, bitcoin::{BroadcastSignedTransaction, WatchForRawTransaction},
bob, monero, bob, monero,
monero::{CreateWalletForOutput, Transfer}, monero::{CreateWalletForOutput, Transfer},
transport::{Receive, Send}, transport::{ReceiveMessage, SendMessage},
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use ecdsa_fun::{ use ecdsa_fun::{
@ -21,7 +21,7 @@ pub async fn next_state<
R: RngCore + CryptoRng, R: RngCore + CryptoRng,
B: WatchForRawTransaction + BroadcastSignedTransaction, B: WatchForRawTransaction + BroadcastSignedTransaction,
M: CreateWalletForOutput + Transfer, M: CreateWalletForOutput + Transfer,
T: Send<Message> + Receive<bob::Message>, T: SendMessage<Message> + ReceiveMessage<bob::Message>,
>( >(
bitcoin_wallet: &B, bitcoin_wallet: &B,
monero_wallet: &M, monero_wallet: &M,

View file

@ -6,7 +6,7 @@ use crate::{
}, },
monero, monero,
monero::{CheckTransfer, CreateWalletForOutput}, monero::{CheckTransfer, CreateWalletForOutput},
transport::{Receive, Send}, transport::{ReceiveMessage, SendMessage},
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use ecdsa_fun::{ use ecdsa_fun::{
@ -25,7 +25,7 @@ pub async fn next_state<
R: RngCore + CryptoRng, R: RngCore + CryptoRng,
B: WatchForRawTransaction + SignTxLock + BuildTxLockPsbt + BroadcastSignedTransaction, B: WatchForRawTransaction + SignTxLock + BuildTxLockPsbt + BroadcastSignedTransaction,
M: CreateWalletForOutput + CheckTransfer, M: CreateWalletForOutput + CheckTransfer,
T: Send<Message> + Receive<alice::Message>, T: SendMessage<Message> + ReceiveMessage<alice::Message>,
>( >(
bitcoin_wallet: &B, bitcoin_wallet: &B,
monero_wallet: &M, monero_wallet: &M,

View file

@ -2,11 +2,11 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
#[async_trait] #[async_trait]
pub trait Send<SendMsg> { pub trait SendMessage<SendMsg> {
async fn send_message(&mut self, message: SendMsg) -> Result<()>; async fn send_message(&mut self, message: SendMsg) -> Result<()>;
} }
#[async_trait] #[async_trait]
pub trait Receive<RecvMsg> { pub trait ReceiveMessage<RecvMsg> {
async fn receive_message(&mut self) -> Result<RecvMsg>; async fn receive_message(&mut self) -> Result<RecvMsg>;
} }

View file

@ -4,10 +4,7 @@ use tokio::{
stream::StreamExt, stream::StreamExt,
sync::mpsc::{Receiver, Sender}, sync::mpsc::{Receiver, Sender},
}; };
use xmr_btc::{ use xmr_btc::transport::{ReceiveMessage, SendMessage};
alice, bob,
transport::{Receive, Send},
};
#[derive(Debug)] #[derive(Debug)]
pub struct Transport<SendMsg, RecvMsg> { pub struct Transport<SendMsg, RecvMsg> {
@ -16,8 +13,12 @@ pub struct Transport<SendMsg, RecvMsg> {
} }
#[async_trait] #[async_trait]
impl Send<alice::Message> for Transport<alice::Message, bob::Message> { impl<SendMsg, RecvMsg> SendMessage<SendMsg> for Transport<SendMsg, RecvMsg>
async fn send_message(&mut self, message: alice::Message) -> Result<()> { where
SendMsg: Send + Sync,
RecvMsg: std::marker::Send,
{
async fn send_message(&mut self, message: SendMsg) -> Result<()> {
let _ = self let _ = self
.sender .sender
.send(message) .send(message)
@ -28,32 +29,12 @@ impl Send<alice::Message> for Transport<alice::Message, bob::Message> {
} }
#[async_trait] #[async_trait]
impl Receive<bob::Message> for Transport<alice::Message, bob::Message> { impl<SendMsg, RecvMsg> ReceiveMessage<RecvMsg> for Transport<SendMsg, RecvMsg>
async fn receive_message(&mut self) -> Result<bob::Message> { where
let message = self SendMsg: std::marker::Send,
.receiver RecvMsg: Send + Sync,
.next() {
.await async fn receive_message(&mut self) -> Result<RecvMsg> {
.ok_or_else(|| anyhow!("failed to receive message"))?;
Ok(message)
}
}
#[async_trait]
impl Send<bob::Message> for Transport<bob::Message, alice::Message> {
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_trait]
impl Receive<alice::Message> for Transport<bob::Message, alice::Message> {
async fn receive_message(&mut self) -> Result<alice::Message> {
let message = self let message = self
.receiver .receiver
.next() .next()