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 #[inline]
37 pub fn new(capacity: Capacity) -> Self {
38 let sender: BroadcastSender<T> = BroadcastSender::new(capacity);
39 let mut broadcast: Broadcast<T> = Broadcast::default();
40 broadcast.sender = sender;
41 broadcast.capacity = capacity;
42 broadcast
43 }
44
45 /// Retrieves the current number of active receivers subscribed to this broadcast channel.
46 ///
47 /// # Returns
48 ///
49 /// - `ReceiverCount` - The total count of active receivers.
50 #[inline]
51 pub fn receiver_count(&self) -> ReceiverCount {
52 self.sender.receiver_count()
53 }
54
55 /// Subscribes a new receiver to the broadcast channel.
56 ///
57 /// # Returns
58 ///
59 /// - `BroadcastReceiver<T>` - A new receiver instance.
60 #[inline]
61 pub fn subscribe(&self) -> BroadcastReceiver<T> {
62 self.sender.subscribe()
63 }
64
65 /// Sends a message to all active receivers subscribed to this broadcast channel.
66 ///
67 /// # Arguments
68 ///
69 /// - `T` - The message to be broadcasted.
70 ///
71 /// # Returns
72 ///
73 /// - `BroadcastSendResult<T>` - Result indicating send status.
74 #[inline]
75 pub fn send(&self, data: T) -> BroadcastSendResult<T> {
76 self.sender.send(data)
77 }
78}