diff options
| author | bors <bors@rust-lang.org> | 2021-07-23 20:26:33 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-07-23 20:26:33 +0000 |
| commit | 67b03007cf40f2331892d5b0f65d2917ac3603d5 (patch) | |
| tree | fc0eeae25f29e324e89f3ad0d11045fcce5f446f /compiler/rustc_data_structures/src | |
| parent | 4a1f419e641c7ec56a60f1714ced5c343e0a2b38 (diff) | |
| parent | a6515816a644be708578328b425a60c7f1e6168e (diff) | |
| download | rust-67b03007cf40f2331892d5b0f65d2917ac3603d5.tar.gz rust-67b03007cf40f2331892d5b0f65d2917ac3603d5.zip | |
Auto merge of #87413 - JohnTitor:rollup-dht22jk, r=JohnTitor
Rollup of 14 pull requests Successful merges: - #86410 (VecMap::get_value_matching should return just one element) - #86790 (Document iteration order of `retain` functions) - #87171 (Remove Option from BufWriter) - #87175 (Stabilize `into_parts()` and `into_error()`) - #87185 (Fix panics on Windows when the build was cancelled) - #87191 (Package LLVM libs for the target rather than the build host) - #87255 (better support for running libcore tests with Miri) - #87266 (Add testcase for 87076) - #87283 (Add `--codegen-backends=foo,bar` configure flag) - #87322 (fix: clarify suggestion that `&T` must refer to `T: Sync` for `&T: Send`) - #87358 (Fix `--dry-run` when download-ci-llvm is set) - #87380 (Don't default to `submodules = true` unless the rust repo has a .git directory) - #87398 (Add test for fonts used for module items) - #87412 (Add missing article) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_data_structures/src')
| -rw-r--r-- | compiler/rustc_data_structures/src/vec_map.rs | 27 |
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); |
