// nice wrapper to have a Default impl for a (Sender, InactiveReceiver) pair use async_broadcast::{broadcast, InactiveReceiver, Receiver, Sender, TrySendError}; pub struct Broadcast { pub sender: Sender, inactive_receiver: InactiveReceiver, } impl Default for Broadcast { fn default() -> Self { let (sender, receiver) = broadcast(N); Self { sender, inactive_receiver: receiver.deactivate(), } } } impl Broadcast where T: Clone { pub fn receiver(&self) -> Receiver { self.inactive_receiver.activate_cloned() } // like sender.try_broadcast, but ignores if there aren't any active listeners pub fn try_broadcast(&self, msg: T) -> Result<(), TrySendError> { match self.sender.try_broadcast(msg) { Err(TrySendError::Inactive(_)) => Ok(()), // ignore Err(err) => Err(err), Ok(Some(_)) => unreachable!("we do not enable overflow mode"), Ok(None) => Ok(()), } } }