From 24efefafcbddfba5d26330d33f2516ae04aa454f Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 9 Apr 2025 13:40:00 +0000 Subject: Avoid a reverse map that is only used in diagnostics paths --- compiler/rustc_data_structures/src/unord.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'compiler/rustc_data_structures') 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> UnordItems { pub fn collect>>(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 { + let item = self.0.next(); + if self.0.next().is_some() { + return None; + } + item + } } impl UnordItems> { -- cgit 1.4.1-3-g733a5