diff --git a/compiler/rustc_data_structures/src/sorted_map/index_map.rs b/compiler/rustc_data_structures/src/sorted_map/index_map.rs index 814e7c7fb9ba6..7d23ff5194870 100644 --- a/compiler/rustc_data_structures/src/sorted_map/index_map.rs +++ b/compiler/rustc_data_structures/src/sorted_map/index_map.rs @@ -100,6 +100,11 @@ impl SortedIndexMultiMap { (k == &key).then_some((i, v)) }) } + + #[inline] + pub fn contains_key(&self, key: K) -> bool { + self.get_by_key(key).next().is_some() + } } impl Eq for SortedIndexMultiMap {} diff --git a/compiler/rustc_data_structures/src/sorted_map/tests.rs b/compiler/rustc_data_structures/src/sorted_map/tests.rs index 3cc250862df42..def7a7112fb3f 100644 --- a/compiler/rustc_data_structures/src/sorted_map/tests.rs +++ b/compiler/rustc_data_structures/src/sorted_map/tests.rs @@ -17,6 +17,10 @@ fn test_sorted_index_multi_map() { assert_eq!(set.get_by_key(3).copied().collect::>(), vec![0]); assert!(set.get_by_key(4).next().is_none()); + // `contains_key` works + assert!(set.contains_key(3)); + assert!(!set.contains_key(4)); + // `get_by_key` returns items in insertion order. let twos: Vec<_> = set.get_by_key_enumerated(2).collect(); let idxs: Vec = twos.iter().map(|(i, _)| *i).collect();