Add IndexedMap::get_key_value
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 12 Dec 2023 20:51:22 +0000 (14:51 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Thu, 11 Apr 2024 00:11:19 +0000 (19:11 -0500)
Useful for applying Option::map when needing both the key and value for
when needing a reference to the key with a longer lifetime.

lightning/src/util/indexed_map.rs

index 4f694bd2b6ec723e5dcaed614db36bdad0ea2aeb..97788ffe68acbd5f90d67bd501710c3b5942ce51 100644 (file)
@@ -56,6 +56,11 @@ impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
                self.map.get_mut(key)
        }
 
+       /// Fetches the key-value pair corresponding to the supplied key, if one exists.
+       pub fn get_key_value(&self, key: &K) -> Option<(&K, &V)> {
+               self.map.get_key_value(key)
+       }
+
        #[inline]
        /// Returns true if an element with the given `key` exists in the map.
        pub fn contains_key(&self, key: &K) -> bool {