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