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    /// - `BroadcastMap<T>` - An empty broadcast map.
16    fn default() -> Self {
17        Self(DashMap::with_hasher(BuildHasherDefault::default()))
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    /// - `BroadcastMap<T>` - An empty broadcast map.
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    /// - `&DashMapStringBroadcast<T>` - Reference to the internal map.
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    /// - `K` - Key convertible to `String`.
52    /// - `Capacity` - Maximum number of buffered messages.
53    ///
54    /// # Returns
55    ///
56    /// - `Option<Broadcast<T>>` - Previous broadcast channel if replaced.
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    /// - `K` - Key convertible to `String`.
71    ///
72    /// # Returns
73    ///
74    /// - `Option<ReceiverCount>` - Number of receivers if channel exists.
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    /// - `K` - Key convertible to `String`.
89    ///
90    /// # Returns
91    ///
92    /// - `Option<BroadcastReceiver<T>>` - New receiver if channel exists.
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    /// - `K` - Key convertible to `String`.
108    /// - `Capacity` - Capacity for new channel if needed.
109    ///
110    /// # Returns
111    ///
112    /// - `BroadcastReceiver<T>` - New receiver for the 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    /// - `K` - Key convertible to `String`.
132    /// - `T` - Message to broadcast.
133    ///
134    /// # Returns
135    ///
136    /// - `Result<Option<ReceiverCount>, SendError<T>>` - Send result with receiver count or error.
137    pub fn send<K: ToString>(&self, key: K, data: T) -> BroadcastMapSendResult<T>
138    where
139        K: ToString,
140    {
141        match self.get().get(&key.to_string()) {
142            Some(sender) => sender.send(data).map(|result| Some(result)),
143            None => Ok(None),
144        }
145    }
146}