hyperlane_broadcast/broadcast/
impl.rs

1use crate::*;
2
3/// Implements the `BroadcastTrait` for any type that also implements `Clone` and `Debug`.
4/// This blanket implementation allows any clonable and debuggable type to be used in the broadcast system.
5impl<T: Clone + Debug> BroadcastTrait for T {}
6
7/// Provides a default implementation for `Broadcast` instances.
8///
9/// The default broadcast channel is initialized with a predefined sender capacity.
10impl<T: BroadcastTrait> Default for Broadcast<T> {
11    /// Creates a new `Broadcast` instance with default settings.
12    ///
13    /// # Returns
14    ///
15    /// - `Broadcast<T>` - A broadcast instance with default sender capacity.
16    fn default() -> Self {
17        let sender: BroadcastSender<T> = BroadcastSender::new(DEFAULT_BROADCAST_SENDER_CAPACITY);
18        Broadcast {
19            capacity: 0,
20            sender,
21        }
22    }
23}
24
25/// Implements core functionalities for the `Broadcast` struct.
26impl<T: BroadcastTrait> Broadcast<T> {
27    /// Creates a new `Broadcast` instance with a specified capacity.
28    ///
29    /// # Arguments
30    ///
31    /// - `Capacity` - The maximum number of messages that can be buffered.
32    ///
33    /// # Returns
34    ///
35    /// - `Broadcast<T>` - A new broadcast instance.
36    pub fn new(capacity: Capacity) -> Self {
37        let sender: BroadcastSender<T> = BroadcastSender::new(capacity);
38        let mut broadcast: Broadcast<T> = Broadcast::default();
39        broadcast.sender = sender;
40        broadcast.capacity = capacity;
41        broadcast
42    }
43
44    /// Retrieves the current number of active receivers subscribed to this broadcast channel.
45    ///
46    /// # Returns
47    ///
48    /// - `ReceiverCount` - The total count of active receivers.
49    pub fn receiver_count(&self) -> ReceiverCount {
50        self.sender.receiver_count()
51    }
52
53    /// Subscribes a new receiver to the broadcast channel.
54    ///
55    /// # Returns
56    ///
57    /// - `BroadcastReceiver<T>` - A new receiver instance.
58    pub fn subscribe(&self) -> BroadcastReceiver<T> {
59        self.sender.subscribe()
60    }
61
62    /// Sends a message to all active receivers subscribed to this broadcast channel.
63    ///
64    /// # Arguments
65    ///
66    /// - `T` - The message to be broadcasted.
67    ///
68    /// # Returns
69    ///
70    /// - `BroadcastSendResult<T>` - Result indicating send status.
71    pub fn send(&self, data: T) -> BroadcastSendResult<T> {
72        self.sender.send(data)
73    }
74}