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    #[inline(always)]
17    fn default() -> Self {
18        let sender: BroadcastSender<T> = BroadcastSender::new(DEFAULT_BROADCAST_SENDER_CAPACITY);
19        Self(sender)
20    }
21}
22
23/// Implements core functionalities for the `Broadcast` struct.
24impl<T: BroadcastTrait> Broadcast<T> {
25    /// Creates a new `Broadcast` instance with a specified capacity.
26    ///
27    /// # Arguments
28    ///
29    /// - `Capacity` - The maximum number of messages that can be buffered.
30    ///
31    /// # Returns
32    ///
33    /// - `Broadcast<T>` - A new broadcast instance.
34    #[inline(always)]
35    pub fn new(capacity: Capacity) -> Self {
36        let sender: BroadcastSender<T> = BroadcastSender::new(capacity);
37        Self(sender)
38    }
39
40    /// Retrieves the current number of active receivers subscribed to this broadcast channel.
41    ///
42    /// # Returns
43    ///
44    /// - `ReceiverCount` - The total count of active receivers.
45    #[inline(always)]
46    pub fn receiver_count(&self) -> ReceiverCount {
47        self.0.receiver_count()
48    }
49
50    /// Subscribes a new receiver to the broadcast channel.
51    ///
52    /// # Returns
53    ///
54    /// - `BroadcastReceiver<T>` - A new receiver instance.
55    #[inline(always)]
56    pub fn subscribe(&self) -> BroadcastReceiver<T> {
57        self.0.subscribe()
58    }
59
60    /// Sends a message to all active receivers subscribed to this broadcast channel.
61    ///
62    /// # Arguments
63    ///
64    /// - `T` - The message to be broadcasted.
65    ///
66    /// # Returns
67    ///
68    /// - `BroadcastSendResult<T>` - Result indicating send status.
69    #[inline(always)]
70    pub fn send(&self, data: T) -> BroadcastSendResult<T> {
71        self.0.send(data)
72    }
73}