diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2023-04-17 13:14:03 +0000 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2023-04-17 14:23:46 +0000 |
| commit | e1cd99f6ff2bc2aecec530801b8a71d898bff309 (patch) | |
| tree | ab542ec50f5d319ee91efccbe69dff94e31dba90 /compiler/rustc_index/src | |
| parent | e49122fb1ca87a6c3e3c22abb315fc75cfe8daed (diff) | |
| download | rust-e1cd99f6ff2bc2aecec530801b8a71d898bff309.tar.gz rust-e1cd99f6ff2bc2aecec530801b8a71d898bff309.zip | |
Make `IndexVec::ensure_contains_elem` return a reference to the element
Diffstat (limited to 'compiler/rustc_index/src')
| -rw-r--r-- | compiler/rustc_index/src/interval.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_index/src/vec.rs | 15 |
2 files changed, 9 insertions, 9 deletions
diff --git a/compiler/rustc_index/src/interval.rs b/compiler/rustc_index/src/interval.rs index d809740c6ab..4605d42a15b 100644 --- a/compiler/rustc_index/src/interval.rs +++ b/compiler/rustc_index/src/interval.rs @@ -261,8 +261,7 @@ impl<R: Idx, C: Step + Idx> SparseIntervalMatrix<R, C> { } fn ensure_row(&mut self, row: R) -> &mut IntervalSet<C> { - self.rows.ensure_contains_elem(row, || IntervalSet::new(self.column_size)); - &mut self.rows[row] + self.rows.ensure_contains_elem(row, || IntervalSet::new(self.column_size)) } pub fn union_row(&mut self, row: R, from: &IntervalSet<C>) -> bool diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index ae2f52c513e..95600f7bcf1 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -236,12 +236,16 @@ impl<I: Idx, T> IndexVec<I, T> { /// `elem`; if that is already true, then has no /// effect. Otherwise, inserts new values as needed by invoking /// `fill_value`. + /// + /// Returns a reference to the `elem` entry. #[inline] - pub fn ensure_contains_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) { + pub fn ensure_contains_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) -> &mut T { let min_new_len = elem.index() + 1; if self.len() < min_new_len { self.raw.resize_with(min_new_len, fill_value); } + + &mut self[elem] } #[inline] @@ -446,20 +450,17 @@ impl<I: Idx, J: Idx> IndexSlice<I, J> { impl<I: Idx, T> IndexVec<I, Option<T>> { #[inline] pub fn insert(&mut self, index: I, value: T) -> Option<T> { - self.ensure_contains_elem(index, || None); - self[index].replace(value) + self.ensure_contains_elem(index, || None).replace(value) } #[inline] pub fn get_or_insert_with(&mut self, index: I, value: impl FnOnce() -> T) -> &mut T { - self.ensure_contains_elem(index, || None); - self[index].get_or_insert_with(value) + self.ensure_contains_elem(index, || None).get_or_insert_with(value) } #[inline] pub fn remove(&mut self, index: I) -> Option<T> { - self.ensure_contains_elem(index, || None); - self[index].take() + self.ensure_contains_elem(index, || None).take() } } |
