hyperlane_broadcast/broadcast_map/
impl.rs

1use crate::*;
2
3/// Implements the `BroadcastMapTrait` for any type that also implements `Clone` and `Debug`.
4/// This blanket implementation allows any clonable and debuggable type to be used as a value in the broadcast map system.
5impl<T: Clone + Debug> BroadcastMapTrait for T {}
6
7/// Provides a default implementation for `BroadcastMap` instances.
8///
9/// The default broadcast map is initialized as an empty `DashMap`.
10impl<T: BroadcastMapTrait> Default for BroadcastMap<T> {
11    /// Creates a new, empty `BroadcastMap` instance.
12    ///
13    /// # Returns
14    ///
15    /// An empty `BroadcastMap`.
16    fn default() -> Self {
17        Self(DashMap::new())
18    }
19}
20
21/// Implements core functionalities for the `BroadcastMap` struct.
22impl<T: BroadcastMapTrait> BroadcastMap<T> {
23    /// Creates a new, empty `BroadcastMap` instance.
24    ///
25    /// This is a convenience constructor that simply calls `default()`.
26    ///
27    /// # Returns
28    ///
29    /// An empty `BroadcastMap`.
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Retrieves an immutable reference to the underlying `DashMapStringBroadcast`.
35    ///
36    /// This private helper method provides direct access to the internal map.
37    ///
38    /// # Returns
39    ///
40    /// A reference to the `DashMapStringBroadcast` containing the broadcast channels.
41    fn get(&self) -> &DashMapStringBroadcast<T> {
42        &self.0
43    }
44
45    /// Inserts a new broadcast channel into the map with a specified key and capacity.
46    ///
47    /// If a broadcast channel with the given key already exists, it will be replaced.
48    ///
49    /// # Arguments
50    ///
51    /// - `key` - The key (convertible to `String`) to associate with the new broadcast channel.
52    /// - `capacity` - The maximum number of messages that can be buffered in the new broadcast channel.
53    ///
54    /// # Returns
55    ///
56    /// An `Option` containing the old `Broadcast` channel if one was replaced, otherwise `None`.
57    pub fn insert<K>(&self, key: K, capacity: Capacity) -> OptionBroadcast<T>
58    where
59        K: ToString,
60    {
61        let key_string: String = key.to_string();
62        let broadcast: Broadcast<T> = Broadcast::new(capacity);
63        self.get().insert(key_string, broadcast)
64    }
65
66    /// Retrieves the number of active receivers for the broadcast channel associated with the given key.
67    ///
68    /// # Arguments
69    ///
70    /// - `key` - The key (convertible to `String`) of the broadcast channel.
71    ///
72    /// # Returns
73    ///
74    /// An `Option` containing the `ReceiverCount` if the broadcast channel exists, otherwise `None`.
75    pub fn receiver_count<K>(&self, key: K) -> OptionReceiverCount
76    where
77        K: ToString,
78    {
79        self.get()
80            .get(&key.to_string())
81            .map(|receiver| receiver.receiver_count())
82    }
83
84    /// Subscribes a new receiver to the broadcast channel associated with the given key.
85    ///
86    /// # Arguments
87    ///
88    /// - `key` - The key (convertible to `String`) of the broadcast channel.
89    ///
90    /// # Returns
91    ///
92    /// An `Option` containing a `BroadcastMapReceiver` if the broadcast channel exists, otherwise `None`.
93    pub fn subscribe<K>(&self, key: K) -> OptionBroadcastMapReceiver<T>
94    where
95        K: ToString,
96    {
97        self.get()
98            .get(&key.to_string())
99            .map(|receiver| receiver.subscribe())
100    }
101
102    /// Subscribes a new receiver to the broadcast channel associated with the given key.
103    /// If the channel does not exist, it will be created with the specified capacity before subscribing.
104    ///
105    /// # Arguments
106    ///
107    /// - `key` - The key (convertible to `String`) of the broadcast channel.
108    /// - `capacity` - The capacity to use if a new broadcast channel needs to be created.
109    ///
110    /// # Returns
111    ///
112    /// A `BroadcastMapReceiver` for the specified broadcast channel.
113    pub fn subscribe_or_insert<K>(&self, key: K, capacity: Capacity) -> BroadcastMapReceiver<T>
114    where
115        K: ToString,
116    {
117        let key_string: String = key.to_string();
118        match self.get().get(&key_string) {
119            Some(sender) => sender.subscribe(),
120            None => {
121                self.insert(key, capacity);
122                self.subscribe_or_insert(key_string, capacity)
123            }
124        }
125    }
126
127    /// Sends a message to the broadcast channel associated with the given key.
128    ///
129    /// # Arguments
130    ///
131    /// - `key` - The key (convertible to `String`) of the broadcast channel.
132    /// - `data` - The message to be broadcasted.
133    ///
134    /// # Returns
135    ///
136    /// A `BroadcastMapSendResult` indicating the number of receivers the message was sent to (if the channel exists),
137    /// or an error if the send operation failed. If the channel does not exist, `Ok(None)` is returned.
138    pub fn send<K: ToString>(&self, key: K, data: T) -> BroadcastMapSendResult<T>
139    where
140        K: ToString,
141    {
142        match self.get().get(&key.to_string()) {
143            Some(sender) => sender.send(data).map(|result| Some(result)),
144            None => Ok(None),
145        }
146    }
147}