Redo layout of eventloop module

1. Move internal types to the bottom and make them private
2. Sort public types by their importance
This commit is contained in:
Thomas Eizinger 2021-03-05 14:01:09 +11:00
parent 1822886cd0
commit b178e95f95
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96

View File

@ -22,57 +22,6 @@ use tokio::sync::{broadcast, mpsc};
use tracing::{debug, error, trace}; use tracing::{debug, error, trace};
use uuid::Uuid; use uuid::Uuid;
#[allow(missing_debug_implementations)]
pub struct MpscChannels<T> {
sender: mpsc::Sender<T>,
receiver: mpsc::Receiver<T>,
}
impl<T> Default for MpscChannels<T> {
fn default() -> Self {
let (sender, receiver) = mpsc::channel(100);
MpscChannels { sender, receiver }
}
}
#[allow(missing_debug_implementations)]
pub struct BroadcastChannels<T>
where
T: Clone,
{
sender: broadcast::Sender<T>,
}
impl<T> Default for BroadcastChannels<T>
where
T: Clone,
{
fn default() -> Self {
let (sender, _receiver) = broadcast::channel(100);
BroadcastChannels { sender }
}
}
#[derive(Debug)]
pub struct EventLoopHandle {
recv_encrypted_signature: broadcast::Receiver<EncryptedSignature>,
send_transfer_proof: mpsc::Sender<(PeerId, TransferProof)>,
}
impl EventLoopHandle {
pub async fn recv_encrypted_signature(&mut self) -> Result<EncryptedSignature> {
self.recv_encrypted_signature
.recv()
.await
.context("Failed to receive Bitcoin encrypted signature from Bob")
}
pub async fn send_transfer_proof(&mut self, bob: PeerId, msg: TransferProof) -> Result<()> {
let _ = self.send_transfer_proof.send((bob, msg)).await?;
Ok(())
}
}
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct EventLoop<RS> { pub struct EventLoop<RS> {
swarm: libp2p::Swarm<Behaviour>, swarm: libp2p::Swarm<Behaviour>,
@ -93,26 +42,10 @@ pub struct EventLoop<RS> {
swap_handle_sender: mpsc::Sender<RemoteHandle<Result<AliceState>>>, swap_handle_sender: mpsc::Sender<RemoteHandle<Result<AliceState>>>,
} }
pub trait LatestRate { #[derive(Debug)]
type Error: std::error::Error + Send + Sync + 'static; pub struct EventLoopHandle {
recv_encrypted_signature: broadcast::Receiver<EncryptedSignature>,
fn latest_rate(&mut self) -> Result<Rate, Self::Error>; send_transfer_proof: mpsc::Sender<(PeerId, TransferProof)>,
}
impl LatestRate for FixedRate {
type Error = Infallible;
fn latest_rate(&mut self) -> Result<Rate, Self::Error> {
Ok(self.value())
}
}
impl LatestRate for kraken::RateUpdateStream {
type Error = kraken::Error;
fn latest_rate(&mut self) -> Result<Rate, Self::Error> {
self.latest_update()
}
} }
impl<LR> EventLoop<LR> impl<LR> EventLoop<LR>
@ -336,9 +269,76 @@ where
} }
} }
pub trait LatestRate {
type Error: std::error::Error + Send + Sync + 'static;
fn latest_rate(&mut self) -> Result<Rate, Self::Error>;
}
impl LatestRate for FixedRate {
type Error = Infallible;
fn latest_rate(&mut self) -> Result<Rate, Self::Error> {
Ok(self.value())
}
}
impl LatestRate for kraken::RateUpdateStream {
type Error = kraken::Error;
fn latest_rate(&mut self) -> Result<Rate, Self::Error> {
self.latest_update()
}
}
impl EventLoopHandle {
pub async fn recv_encrypted_signature(&mut self) -> Result<EncryptedSignature> {
self.recv_encrypted_signature
.recv()
.await
.context("Failed to receive Bitcoin encrypted signature from Bob")
}
pub async fn send_transfer_proof(&mut self, bob: PeerId, msg: TransferProof) -> Result<()> {
let _ = self.send_transfer_proof.send((bob, msg)).await?;
Ok(())
}
}
#[derive(Debug, Clone, Copy, thiserror::Error)] #[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("Refusing to buy {actual} because the maximum configured limit is {max}")] #[error("Refusing to buy {actual} because the maximum configured limit is {max}")]
pub struct MaximumBuyAmountExceeded { pub struct MaximumBuyAmountExceeded {
pub max: bitcoin::Amount, pub max: bitcoin::Amount,
pub actual: bitcoin::Amount, pub actual: bitcoin::Amount,
} }
#[allow(missing_debug_implementations)]
struct MpscChannels<T> {
sender: mpsc::Sender<T>,
receiver: mpsc::Receiver<T>,
}
impl<T> Default for MpscChannels<T> {
fn default() -> Self {
let (sender, receiver) = mpsc::channel(100);
MpscChannels { sender, receiver }
}
}
#[allow(missing_debug_implementations)]
struct BroadcastChannels<T>
where
T: Clone,
{
sender: broadcast::Sender<T>,
}
impl<T> Default for BroadcastChannels<T>
where
T: Clone,
{
fn default() -> Self {
let (sender, _receiver) = broadcast::channel(100);
BroadcastChannels { sender }
}
}