tiny_tokio_actor/
bus.rs

1use tokio::sync::broadcast;
2pub use tokio::sync::broadcast::error::RecvError as EventRecvError;
3pub use tokio::sync::broadcast::error::SendError;
4use tokio::sync::broadcast::{Receiver as BroadcastReceiver, Sender as BroadcastSender};
5
6pub type EventReceiver<T> = BroadcastReceiver<T>;
7pub(crate) type EventSender<T> = BroadcastSender<T>;
8
9#[derive(Clone)]
10pub struct EventBus<T: Clone> {
11    tx: EventSender<T>,
12}
13
14impl<T: Clone> EventBus<T> {
15    pub fn subscribe(&self) -> EventReceiver<T> {
16        self.tx.subscribe()
17    }
18
19    pub fn send(&self, event: T) -> Result<usize, SendError<T>> {
20        self.tx.send(event)
21    }
22
23    pub fn new(capacity: usize) -> Self {
24        let (tx, _) = broadcast::channel(capacity);
25        EventBus { tx }
26    }
27}