1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Async extension trait for collecting the keys/values of a `tokio::sync::RwLock<HashMap<K, V>>`.
//!
//! Several `list_*`/`get_*` accessors across the workspace (`Runtime::list_nodes`,
//! `TopicRegistry::list_topics`/`get_all_topics`, `Launcher::list_types`,
//! `Supervisor::get_running_nodes`, `FleetCoordinator::list_members`,
//! `HardwareDetector::get_devices`, `CommandExecutor::list_commands`) independently hand-wrote
//! the same `let guard = self.field.read().await; guard.keys().cloned().collect()` (or
//! `.values()`) sequence to snapshot a lock-guarded map into a `Vec`. cargo-dupes flagged this as
//! an 8-member exact-duplicate group (LAB-2361). This trait collapses that boilerplate to a
//! single call so call sites read as `self.field.cloned_keys().await`.
//!
//! `cloned_get` covers the single-key sibling of the same pattern
//! (`HealthChecker::check_one`, `HardwareDetector::get_device`, `FleetCoordinator::get_task`,
//! `TopicRegistry::get_topic`, ...): `let guard = self.field.read().await; guard.get(key).cloned()`.
use std::collections::HashMap;
use std::hash::Hash;
use tokio::sync::RwLock;
/// Snapshot helpers for a lock-guarded `HashMap`.
///
/// Implemented for `RwLock<HashMap<K, V>>` so any struct storing shared state behind a
/// `tokio::sync::RwLock` can collect a cloned `Vec` of its keys/values, or look up a single
/// cloned entry, without re-deriving the read-lock-then-collect boilerplate at each call site.
pub trait RwLockMapExt<K, V> {
/// Read-lock the map and clone all keys into a `Vec`.
fn cloned_keys(&self) -> impl std::future::Future<Output = Vec<K>> + Send
where
K: Clone;
/// Read-lock the map and clone all values into a `Vec`.
fn cloned_values(&self) -> impl std::future::Future<Output = Vec<V>> + Send
where
V: Clone;
/// Read-lock the map and return a clone of the value for `key`, if present.
fn cloned_get<'a>(&'a self, key: &'a K) -> impl std::future::Future<Output = Option<V>> + Send
where
V: Clone;
}
impl<K, V> RwLockMapExt<K, V> for RwLock<HashMap<K, V>>
where
K: Eq + Hash + Send + Sync,
V: Send + Sync,
{
// duplicate: false positive — `cloned_keys`/`cloned_values`/`cloned_get` are themselves the
// shared helper this module exists to provide (see the module doc); `.keys()` vs `.values()`
// vs `.get(key)` is the one irreducible difference, there's no further level to extract to.
async fn cloned_keys(&self) -> Vec<K>
where
K: Clone,
{
self.read().await.keys().cloned().collect()
}
async fn cloned_values(&self) -> Vec<V>
where
V: Clone,
{
self.read().await.values().cloned().collect()
}
async fn cloned_get(&self, key: &K) -> Option<V>
where
V: Clone,
{
self.read().await.get(key).cloned()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn cloned_keys_returns_all_keys() {
let mut map = HashMap::new();
map.insert("a".to_string(), 1);
map.insert("b".to_string(), 2);
let lock = RwLock::new(map);
let mut keys = lock.cloned_keys().await;
keys.sort();
assert_eq!(keys, vec!["a".to_string(), "b".to_string()]);
}
#[tokio::test]
async fn cloned_values_returns_all_values() {
let mut map = HashMap::new();
map.insert("a".to_string(), 1);
map.insert("b".to_string(), 2);
let lock = RwLock::new(map);
let mut values = lock.cloned_values().await;
values.sort();
assert_eq!(values, vec![1, 2]);
}
#[tokio::test]
async fn cloned_keys_empty_map() {
let lock: RwLock<HashMap<String, i32>> = RwLock::new(HashMap::new());
assert!(lock.cloned_keys().await.is_empty());
}
#[tokio::test]
async fn cloned_get_returns_a_clone_of_the_value_for_an_existing_key() {
let mut map = HashMap::new();
map.insert("a".to_string(), 1);
map.insert("b".to_string(), 2);
let lock = RwLock::new(map);
assert_eq!(lock.cloned_get(&"a".to_string()).await, Some(1));
}
#[tokio::test]
async fn cloned_get_returns_none_for_a_missing_key() {
let mut map = HashMap::new();
map.insert("a".to_string(), 1);
let lock = RwLock::new(map);
assert_eq!(lock.cloned_get(&"missing".to_string()).await, None);
}
}