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    #[inline]
31    pub fn new() -> Self {
32        Self::default()
33    }
34
35    /// Retrieves an immutable reference to the underlying `DashMapStringBroadcast`.
36    ///
37    /// This private helper method provides direct access to the internal map.
38    ///
39    /// # Returns
40    ///
41    /// - `&DashMapStringBroadcast<T>` - Reference to the internal map.
42    #[inline]
43    fn get(&self) -> &DashMapStringBroadcast<T> {
44        &self.0
45    }
46
47    /// Inserts a new broadcast channel into the map with a specified key and capacity.
48    ///
49    /// If a broadcast channel with the given key already exists, it will be replaced.
50    ///
51    /// # Arguments
52    ///
53    /// - `AsRef<str>` - Key convertible to `str`.
54    /// - `capacity` - Maximum number of buffered messages.
55    ///
56    /// # Returns
57    ///
58    /// - `Option<Broadcast<T>>` - Previous broadcast channel if replaced.
59    #[inline]
60    pub fn insert<K>(&self, key: K, capacity: Capacity) -> OptionBroadcast<T>
61    where
62        K: AsRef<str>,
63    {
64        let broadcast: Broadcast<T> = Broadcast::new(capacity);
65        self.get().insert(key.as_ref().to_owned(), broadcast)
66    }
67
68    /// Retrieves the number of active receivers for the broadcast channel associated with the given key.
69    ///
70    /// # Arguments
71    ///
72    /// - `AsRef<str>` - Key convertible to `str`.
73    ///
74    /// # Returns
75    ///
76    /// - `Option<ReceiverCount>` - Number of receivers if channel exists.
77    #[inline]
78    pub fn receiver_count<K>(&self, key: K) -> OptionReceiverCount
79    where
80        K: AsRef<str>,
81    {
82        self.get()
83            .get(key.as_ref())
84            .map(|receiver| receiver.receiver_count())
85    }
86
87    /// Subscribes a new receiver to the broadcast channel associated with the given key.
88    ///
89    /// # Arguments
90    ///
91    /// - `AsRef<str>` - Key convertible to `str`.
92    ///
93    /// # Returns
94    ///
95    /// - `Option<BroadcastReceiver<T>>` - New receiver if channel exists.
96    #[inline]
97    pub fn subscribe<K>(&self, key: K) -> OptionBroadcastMapReceiver<T>
98    where
99        K: AsRef<str>,
100    {
101        self.get()
102            .get(key.as_ref())
103            .map(|receiver| receiver.subscribe())
104    }
105
106    /// Subscribes a new receiver to the broadcast channel associated with the given key.
107    /// If the channel does not exist, it will be created with the specified capacity before subscribing.
108    ///
109    /// # Arguments
110    ///
111    /// - `AsRef<str>` - Key convertible to `str`.
112    /// - `capacity` - Capacity for new channel if needed.
113    ///
114    /// # Returns
115    ///
116    /// - `BroadcastReceiver<T>` - New receiver for the channel.
117    #[inline]
118    pub fn subscribe_or_insert<K>(&self, key: K, capacity: Capacity) -> BroadcastMapReceiver<T>
119    where
120        K: AsRef<str>,
121    {
122        let key_ref: &str = key.as_ref();
123        match self.get().get(key_ref) {
124            Some(sender) => sender.subscribe(),
125            None => {
126                self.insert(key_ref, capacity);
127                self.subscribe_or_insert(key_ref, capacity)
128            }
129        }
130    }
131
132    /// Sends a message to the broadcast channel associated with the given key.
133    ///
134    /// # Arguments
135    ///
136    /// - `AsRef<str>` - Key convertible to `str`.
137    /// - `data` - Message to broadcast.
138    ///
139    /// # Returns
140    ///
141    /// - `Result<Option<ReceiverCount>, SendError<T>>` - Send result with receiver count or error.
142    #[inline]
143    pub fn send<K: AsRef<str>>(&self, key: K, data: T) -> BroadcastMapSendResult<T> {
144        match self.get().get(key.as_ref()) {
145            Some(sender) => sender.send(data).map(Some),
146            None => Ok(None),
147        }
148    }
149}