about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src
diff options
context:
space:
mode:
authorOli Scherer <github333195615777966@oli-obk.de>2025-04-09 13:40:00 +0000
committerOli Scherer <github333195615777966@oli-obk.de>2025-04-11 09:33:38 +0000
commit24efefafcbddfba5d26330d33f2516ae04aa454f (patch)
treeb78e7157fb8598f31a57f3cc734ce93b8919449c /compiler/rustc_data_structures/src
parent18a029cfe8e761533634bda8fe8a14e30280a21b (diff)
downloadrust-24efefafcbddfba5d26330d33f2516ae04aa454f.tar.gz
rust-24efefafcbddfba5d26330d33f2516ae04aa454f.zip
Avoid a reverse map that is only used in diagnostics paths
Diffstat (limited to 'compiler/rustc_data_structures/src')
-rw-r--r--compiler/rustc_data_structures/src/unord.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/unord.rs b/compiler/rustc_data_structures/src/unord.rs
index baa66cd7c85..3d44fb1fd48 100644
--- a/compiler/rustc_data_structures/src/unord.rs
+++ b/compiler/rustc_data_structures/src/unord.rs
@@ -109,6 +109,16 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
     pub fn collect<C: From<UnordItems<T, I>>>(self) -> C {
         self.into()
     }
+
+    /// If the iterator has only one element, returns it, otherwise returns `None`.
+    #[track_caller]
+    pub fn get_only(mut self) -> Option<T> {
+        let item = self.0.next();
+        if self.0.next().is_some() {
+            return None;
+        }
+        item
+    }
 }
 
 impl<T> UnordItems<T, std::iter::Empty<T>> {