diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2022-09-29 21:07:06 +0200 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2022-10-01 16:22:40 +0200 |
| commit | d08669c4fa51a864a46438c2e4bc9047931e1033 (patch) | |
| tree | a815b3ba8cecfefb3032b4d3624c6c414ade1b0b /compiler/rustc_data_structures/src | |
| parent | 26e5fe9e8507f7dcb50c21e785515a7841e8cf55 (diff) | |
| download | rust-d08669c4fa51a864a46438c2e4bc9047931e1033.tar.gz rust-d08669c4fa51a864a46438c2e4bc9047931e1033.zip | |
Compute by owner instead of HirId.
Diffstat (limited to 'compiler/rustc_data_structures/src')
| -rw-r--r-- | compiler/rustc_data_structures/src/vec_map.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/vec_map.rs b/compiler/rustc_data_structures/src/vec_map.rs index 86be0bd8775..f1493e5bdad 100644 --- a/compiler/rustc_data_structures/src/vec_map.rs +++ b/compiler/rustc_data_structures/src/vec_map.rs @@ -53,6 +53,20 @@ where self.0.iter_mut().find(|(key, _)| k == key.borrow()).map(|elem| &mut elem.1) } + /// Gets a mutable reference to the value in the entry, or insert a new one. + pub fn get_mut_or_insert_default(&mut self, k: K) -> &mut V + where + K: Eq, + V: Default, + { + let pos = self.0.iter().position(|(key, _)| &k == key).unwrap_or_else(|| { + let pos = self.0.len(); + self.0.push((k, V::default())); + pos + }); + &mut self.0[pos].1 + } + /// 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 |
