]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_data_structures/src/sorted_map.rs
Auto merge of #103691 - michaelwoerister:consistent-slice-and-str-cpp-like-debuginfo...
[rust.git] / compiler / rustc_data_structures / src / sorted_map.rs
index 937cb671573a1c97b2096d3dab4382075365a97e..fe257e10205faaba0a4ed055ec7faa3fd5ac4c7a 100644 (file)
@@ -96,6 +96,23 @@ pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
         }
     }
 
+    /// Gets a mutable reference to the value in the entry, or insert a new one.
+    #[inline]
+    pub fn get_mut_or_insert_default(&mut self, key: K) -> &mut V
+    where
+        K: Eq,
+        V: Default,
+    {
+        let index = match self.lookup_index_for(&key) {
+            Ok(index) => index,
+            Err(index) => {
+                self.data.insert(index, (key, V::default()));
+                index
+            }
+        };
+        unsafe { &mut self.data.get_unchecked_mut(index).1 }
+    }
+
     #[inline]
     pub fn clear(&mut self) {
         self.data.clear();