use futures::executor::block_on; use tokio::sync::broadcast; use crate::message::Message; #[derive(Clone, Debug, PartialEq)] #[non_exhaustive] pub enum RecvError { Empty, Closed, Lagged, } impl From for RecvError { fn from(err: broadcast::error::RecvError) -> Self { match err { broadcast::error::RecvError::Closed => RecvError::Closed, broadcast::error::RecvError::Lagged(_) => RecvError::Lagged, } } } impl From for RecvError { fn from(err: broadcast::error::TryRecvError) -> Self { match err { broadcast::error::TryRecvError::Empty => RecvError::Empty, broadcast::error::TryRecvError::Closed => RecvError::Closed, broadcast::error::TryRecvError::Lagged(_) => RecvError::Lagged, } } } #[derive(Debug)] pub struct Subscription { receiver: broadcast::Receiver, } impl Subscription { pub(crate) fn new(receiver: broadcast::Receiver) -> Self { Subscription { receiver } } } impl Subscription { pub async fn recv(&mut self) -> Result { Ok(self.receiver.recv().await?) } pub fn recv_blocking(&mut self) -> Result { block_on(self.recv()) } pub fn try_recv(&mut self) -> Result { Ok(self.receiver.try_recv()?) } }