Alex Mikhalev
4 years ago
7 changed files with 433 additions and 47 deletions
@ -0,0 +1,5 @@
@@ -0,0 +1,5 @@
|
||||
mod receiver; |
||||
mod sender; |
||||
|
||||
pub use receiver::{Receiver, ReceiverError}; |
||||
pub use sender::{Sender, SenderError}; |
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
use futures::FutureExt; |
||||
use tokio::sync::mpsc; |
||||
|
||||
use crate::message::Message; |
||||
|
||||
#[derive(Clone, Debug, PartialEq)] |
||||
#[non_exhaustive] |
||||
pub enum ReceiverError { |
||||
Empty, |
||||
Closed, |
||||
} |
||||
|
||||
impl From<mpsc::error::RecvError> for ReceiverError { |
||||
fn from(_: mpsc::error::RecvError) -> Self { |
||||
ReceiverError::Closed |
||||
} |
||||
} |
||||
|
||||
// impl From<mpsc::error::TryRecvError> for ReceiverError {
|
||||
// fn from(err: mpsc::error::TryRecvError) -> Self {
|
||||
// match err {
|
||||
// mpsc::error::TryRecvError::Empty => ReceiverError::Empty,
|
||||
// mpsc::error::TryRecvError::Closed => ReceiverError::Closed,
|
||||
// mpsc::error::TryRecvError::Lagged(_) => ReceiverError::Lagged,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Debug)] |
||||
pub struct Receiver<T> { |
||||
receiver: mpsc::Receiver<T>, |
||||
} |
||||
|
||||
impl<T: Message> Receiver<T> { |
||||
pub(crate) fn new(receiver: mpsc::Receiver<T>) -> Self { |
||||
Receiver { receiver } |
||||
} |
||||
|
||||
pub async fn recv(&mut self) -> Result<T, ReceiverError> { |
||||
self.receiver.recv().await.ok_or(ReceiverError::Closed) |
||||
} |
||||
|
||||
pub fn recv_blocking(&mut self) -> Result<T, ReceiverError> { |
||||
self.receiver.blocking_recv().ok_or(ReceiverError::Closed) |
||||
} |
||||
|
||||
pub fn try_recv(&mut self) -> Result<T, ReceiverError> { |
||||
// Ok(self.receiver.try_recv()?)
|
||||
self.recv() |
||||
.now_or_never() |
||||
.unwrap_or(Err(ReceiverError::Empty)) |
||||
} |
||||
} |
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
use tokio::sync::mpsc; |
||||
|
||||
use crate::message::Message; |
||||
|
||||
#[derive(Clone, Debug, PartialEq)] |
||||
#[non_exhaustive] |
||||
pub enum SenderError { |
||||
Closed, |
||||
Full |
||||
} |
||||
|
||||
impl<T> From<mpsc::error::SendError<T>> for SenderError { |
||||
fn from(_: mpsc::error::SendError<T>) -> Self { |
||||
SenderError::Closed |
||||
} |
||||
} |
||||
|
||||
impl<T> From<mpsc::error::TrySendError<T>> for SenderError { |
||||
fn from(err: mpsc::error::TrySendError<T>) -> Self { |
||||
match err { |
||||
mpsc::error::TrySendError::Full(_) => SenderError::Full, |
||||
mpsc::error::TrySendError::Closed(_) => SenderError::Closed |
||||
} |
||||
} |
||||
} |
||||
|
||||
#[derive(Clone, Debug)] |
||||
pub struct Sender<T> { |
||||
sender: mpsc::Sender<T>, |
||||
} |
||||
|
||||
impl<T: Message> Sender<T> { |
||||
pub(crate) fn new(sender: mpsc::Sender<T>) -> Self { |
||||
Sender { sender } |
||||
} |
||||
|
||||
pub async fn send(&mut self, message: T) -> Result<(), SenderError> { |
||||
self.sender.send(message).await?; |
||||
Ok(()) |
||||
} |
||||
|
||||
pub fn send_blocking(&mut self, message: T) -> Result<(), SenderError> { |
||||
self.sender.blocking_send(message)?; |
||||
Ok(()) |
||||
} |
||||
|
||||
pub fn try_send(&mut self, message: T) -> Result<(), SenderError> { |
||||
self.sender.try_send(message)?; |
||||
Ok(()) |
||||
} |
||||
} |
Loading…
Reference in new issue