about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src/vec_map.rs
diff options
context:
space:
mode:
authorSantiago Pastorino <spastorino@gmail.com>2021-06-17 16:59:00 -0300
committerSantiago Pastorino <spastorino@gmail.com>2021-07-23 08:44:23 -0300
commitd71410757d1e44dbcfb83ad2cfe75d687c695b91 (patch)
treed2513ff08f27d9e965ef97b7ba46f3d4c40e476c /compiler/rustc_data_structures/src/vec_map.rs
parentb2b7c859c1aae39d26884e760201f5e6c7feeff9 (diff)
downloadrust-d71410757d1e44dbcfb83ad2cfe75d687c695b91.tar.gz
rust-d71410757d1e44dbcfb83ad2cfe75d687c695b91.zip
Add VecMap::get_value_matching and assert if > 1 element
Otherwise is a bug that we want to uncover.
Diffstat (limited to 'compiler/rustc_data_structures/src/vec_map.rs')
-rw-r--r--compiler/rustc_data_structures/src/vec_map.rs27
1 files changed, 23 insertions, 4 deletions
diff --git a/compiler/rustc_data_structures/src/vec_map.rs b/compiler/rustc_data_structures/src/vec_map.rs
index e3fa587985d..cc7ec9432fa 100644
--- a/compiler/rustc_data_structures/src/vec_map.rs
+++ b/compiler/rustc_data_structures/src/vec_map.rs
@@ -1,4 +1,5 @@
 use std::borrow::Borrow;
+use std::fmt::Debug;
 use std::iter::FromIterator;
 use std::slice::Iter;
 use std::vec::IntoIter;
@@ -12,7 +13,8 @@ pub struct VecMap<K, V>(Vec<(K, V)>);
 
 impl<K, V> VecMap<K, V>
 where
-    K: PartialEq,
+    K: Debug + PartialEq,
+    V: Debug,
 {
     pub fn new() -> Self {
         VecMap(Default::default())
@@ -37,14 +39,31 @@ where
         self.0.iter().find(|(key, _)| k == key.borrow()).map(|elem| &elem.1)
     }
 
-    /// Returns the value corresponding to the supplied predicate filter.
+    /// Returns the any value corresponding to the supplied predicate filter.
     ///
     /// The supplied predicate will be applied to each (key, value) pair and it will return a
     /// reference to the values where the predicate returns `true`.
-    pub fn get_by(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
+    pub fn any_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
         self.0.iter().find(|kv| predicate(kv)).map(|elem| &elem.1)
     }
 
+    /// Returns the value corresponding to the supplied predicate filter. It crashes if there's
+    /// more than one matching element.
+    ///
+    /// The supplied predicate will be applied to each (key, value) pair and it will return a
+    /// reference to the value where the predicate returns `true`.
+    pub fn get_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
+        let mut filter = self.0.iter().filter(|kv| predicate(kv));
+        let (_, value) = filter.next()?;
+        // This should return just one element, otherwise it's a bug
+        assert!(
+            filter.next().is_none(),
+            "Collection {:?} should have just one matching element",
+            self
+        );
+        Some(value)
+    }
+
     /// Returns `true` if the map contains a value for the specified key.
     ///
     /// The key may be any borrowed form of the map's key type,
@@ -131,7 +150,7 @@ impl<K, V> IntoIterator for VecMap<K, V> {
     }
 }
 
-impl<K: PartialEq, V> Extend<(K, V)> for VecMap<K, V> {
+impl<K: PartialEq + Debug, V: Debug> Extend<(K, V)> for VecMap<K, V> {
     fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
         for (k, v) in iter {
             self.insert(k, v);