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    /// - `AsRef<str>` - Key convertible to `str`.
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: AsRef<str>,
60    {
61        let broadcast: Broadcast<T> = Broadcast::new(capacity);
62        self.get().insert(key.as_ref().to_owned(), broadcast)
63    }
64
65    /// Retrieves the number of active receivers for the broadcast channel associated with the given key.
66    ///
67    /// # Arguments
68    ///
69    /// - `AsRef<str>` - Key convertible to `str`.
70    ///
71    /// # Returns
72    ///
73    /// - `Option<ReceiverCount>` - Number of receivers if channel exists.
74    pub fn receiver_count<K>(&self, key: K) -> OptionReceiverCount
75    where
76        K: AsRef<str>,
77    {
78        self.get()
79            .get(key.as_ref())
80            .map(|receiver| receiver.receiver_count())
81    }
82
83    /// Subscribes a new receiver to the broadcast channel associated with the given key.
84    ///
85    /// # Arguments
86    ///
87    /// - `AsRef<str>` - Key convertible to `str`.
88    ///
89    /// # Returns
90    ///
91    /// - `Option<BroadcastReceiver<T>>` - New receiver if channel exists.
92    pub fn subscribe<K>(&self, key: K) -> OptionBroadcastMapReceiver<T>
93    where
94        K: AsRef<str>,
95    {
96        self.get()
97            .get(key.as_ref())
98            .map(|receiver| receiver.subscribe())
99    }
100
101    /// Subscribes a new receiver to the broadcast channel associated with the given key.
102    /// If the channel does not exist, it will be created with the specified capacity before subscribing.
103    ///
104    /// # Arguments
105    ///
106    /// - `AsRef<str>` - Key convertible to `str`.
107    /// - `capacity` - Capacity for new channel if needed.
108    ///
109    /// # Returns
110    ///
111    /// - `BroadcastReceiver<T>` - New receiver for the channel.
112    pub fn subscribe_or_insert<K>(&self, key: K, capacity: Capacity) -> BroadcastMapReceiver<T>
113    where
114        K: AsRef<str>,
115    {
116        let key_ref: &str = key.as_ref();
117        match self.get().get(key_ref) {
118            Some(sender) => sender.subscribe(),
119            None => {
120                self.insert(key_ref, capacity);
121                self.subscribe_or_insert(key_ref, capacity)
122            }
123        }
124    }
125
126    /// Sends a message to the broadcast channel associated with the given key.
127    ///
128    /// # Arguments
129    ///
130    /// - `AsRef<str>` - Key convertible to `str`.
131    /// - `data` - Message to broadcast.
132    ///
133    /// # Returns
134    ///
135    /// - `Result<Option<ReceiverCount>, SendError<T>>` - Send result with receiver count or error.
136    pub fn send<K: AsRef<str>>(&self, key: K, data: T) -> BroadcastMapSendResult<T>
137    where
138        K: AsRef<str>,
139    {
140        match self.get().get(key.as_ref()) {
141            Some(sender) => sender.send(data).map(|result| Some(result)),
142            None => Ok(None),
143        }
144    }
145}