orsb/src/publication.rs

26 lines
566 B
Rust

use tokio::sync::broadcast;
use crate::message::Message;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum PublishError {}
#[derive(Debug)]
pub struct Publication<T> {
sender: broadcast::Sender<T>,
}
impl<T: Message> Publication<T> {
pub(crate) fn new(sender: broadcast::Sender<T>) -> Self {
Publication { sender }
}
pub fn publish(&mut self, message: T) -> Result<usize, PublishError> {
match self.sender.send(message) {
Ok(subscribers) => Ok(subscribers),
Err(_) => Ok(0),
}
}
}