Browse Source

Rename RecvError to SubscriptionError

master
Alex Mikhalev 4 years ago
parent
commit
22283dc7ca
  1. 6
      src/lib.rs
  2. 22
      src/subscription.rs

6
src/lib.rs

@ -9,7 +9,7 @@ use broker_task::{
}; };
pub use message::Message; pub use message::Message;
pub use publication::{Publication, PublishError}; pub use publication::{Publication, PublishError};
pub use subscription::{RecvError, Subscription}; pub use subscription::{Subscription, SubscriptionError};
use futures::executor::block_on; use futures::executor::block_on;
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -105,7 +105,7 @@ mod test {
assert_eq!(sub.recv_blocking(), Ok(TestMsg(10))); assert_eq!(sub.recv_blocking(), Ok(TestMsg(10)));
assert_eq!(sub.recv_blocking(), Ok(TestMsg(20))); assert_eq!(sub.recv_blocking(), Ok(TestMsg(20)));
assert_eq!(sub.recv_blocking(), Ok(TestMsg(30))); assert_eq!(sub.recv_blocking(), Ok(TestMsg(30)));
assert_eq!(sub.try_recv(), Err(RecvError::Empty)); assert_eq!(sub.try_recv(), Err(SubscriptionError::Empty));
}) })
.await .await
.unwrap(); .unwrap();
@ -127,6 +127,6 @@ mod test {
assert_eq!(sub.recv().await, Ok(TestMsg(10))); assert_eq!(sub.recv().await, Ok(TestMsg(10)));
assert_eq!(sub.recv().await, Ok(TestMsg(20))); assert_eq!(sub.recv().await, Ok(TestMsg(20)));
assert_eq!(sub.recv().await, Ok(TestMsg(30))); assert_eq!(sub.recv().await, Ok(TestMsg(30)));
assert_eq!(sub.try_recv(), Err(RecvError::Empty)); assert_eq!(sub.try_recv(), Err(SubscriptionError::Empty));
} }
} }

22
src/subscription.rs

@ -5,27 +5,27 @@ use crate::message::Message;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
#[non_exhaustive] #[non_exhaustive]
pub enum RecvError { pub enum SubscriptionError {
Empty, Empty,
Closed, Closed,
Lagged, Lagged,
} }
impl From<broadcast::error::RecvError> for RecvError { impl From<broadcast::error::RecvError> for SubscriptionError {
fn from(err: broadcast::error::RecvError) -> Self { fn from(err: broadcast::error::RecvError) -> Self {
match err { match err {
broadcast::error::RecvError::Closed => RecvError::Closed, broadcast::error::RecvError::Closed => SubscriptionError::Closed,
broadcast::error::RecvError::Lagged(_) => RecvError::Lagged, broadcast::error::RecvError::Lagged(_) => SubscriptionError::Lagged,
} }
} }
} }
impl From<broadcast::error::TryRecvError> for RecvError { impl From<broadcast::error::TryRecvError> for SubscriptionError {
fn from(err: broadcast::error::TryRecvError) -> Self { fn from(err: broadcast::error::TryRecvError) -> Self {
match err { match err {
broadcast::error::TryRecvError::Empty => RecvError::Empty, broadcast::error::TryRecvError::Empty => SubscriptionError::Empty,
broadcast::error::TryRecvError::Closed => RecvError::Closed, broadcast::error::TryRecvError::Closed => SubscriptionError::Closed,
broadcast::error::TryRecvError::Lagged(_) => RecvError::Lagged, broadcast::error::TryRecvError::Lagged(_) => SubscriptionError::Lagged,
} }
} }
} }
@ -42,15 +42,15 @@ impl<T> Subscription<T> {
} }
impl<T: Message> Subscription<T> { impl<T: Message> Subscription<T> {
pub async fn recv(&mut self) -> Result<T, RecvError> { pub async fn recv(&mut self) -> Result<T, SubscriptionError> {
Ok(self.receiver.recv().await?) Ok(self.receiver.recv().await?)
} }
pub fn recv_blocking(&mut self) -> Result<T, RecvError> { pub fn recv_blocking(&mut self) -> Result<T, SubscriptionError> {
block_on(self.recv()) block_on(self.recv())
} }
pub fn try_recv(&mut self) -> Result<T, RecvError> { pub fn try_recv(&mut self) -> Result<T, SubscriptionError> {
Ok(self.receiver.try_recv()?) Ok(self.receiver.try_recv()?)
} }
} }

Loading…
Cancel
Save