diff options
Diffstat (limited to 'compiler/rustc_data_structures/src')
| -rw-r--r-- | compiler/rustc_data_structures/src/unord.rs | 10 |
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>> { |
