Skip to content

Commit 2bdb10f

Browse files
authored
Rollup merge of #138372 - Eclips4:issue-138196, r=scottmcm
Refactor `pick2_mut` & `pick3_mut` to use `get_disjoint_mut` Closes#138196
2 parents b849aa9 + 7398b39 commit 2bdb10f

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

‎compiler/rustc_index/src/slice.rs‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22
use std::marker::PhantomData;
33
use std::ops::{Index,IndexMut};
4+
use std::slice::GetDisjointMutError::*;
45
use std::slice::{self,SliceIndex};
56

67
usecrate::{Idx,IndexVec,IntoSliceIdx};
@@ -121,32 +122,36 @@ impl<I: Idx, T> IndexSlice<I, T> {
121122

122123
/// Returns mutable references to two distinct elements, `a` and `b`.
123124
///
124-
/// Panics if `a == b`.
125+
/// Panics if `a == b` or if some of them are out of bounds.
125126
#[inline]
126127
pubfnpick2_mut(&mutself,a:I,b:I) -> (&mutT,&mutT){
127128
let(ai, bi) = (a.index(), b.index());
128-
assert!(ai != bi);
129-
130-
if ai < bi {
131-
let(c1, c2) = self.raw.split_at_mut(bi);
132-
(&mut c1[ai],&mut c2[0])
133-
}else{
134-
let(c2, c1) = self.pick2_mut(b, a);
135-
(c1, c2)
129+
130+
matchself.raw.get_disjoint_mut([ai, bi]){
131+
Ok([a, b]) => (a, b),
132+
Err(OverlappingIndices) => panic!("Indices {ai:?} and {bi:?} are not disjoint!"),
133+
Err(IndexOutOfBounds) => {
134+
panic!("Some indices among ({ai:?}, {bi:?}) are out of bounds")
135+
}
136136
}
137137
}
138138

139139
/// Returns mutable references to three distinct elements.
140140
///
141-
/// Panics if the elements are not distinct.
141+
/// Panics if the elements are not distinct or if some of them are out of bounds.
142142
#[inline]
143143
pubfnpick3_mut(&mutself,a:I,b:I,c:I) -> (&mutT,&mutT,&mutT){
144144
let(ai, bi, ci) = (a.index(), b.index(), c.index());
145-
assert!(ai != bi && bi != ci && ci != ai);
146-
let len = self.raw.len();
147-
assert!(ai < len && bi < len && ci < len);
148-
let ptr = self.raw.as_mut_ptr();
149-
unsafe{(&mut*ptr.add(ai),&mut*ptr.add(bi),&mut*ptr.add(ci))}
145+
146+
matchself.raw.get_disjoint_mut([ai, bi, ci]){
147+
Ok([a, b, c]) => (a, b, c),
148+
Err(OverlappingIndices) => {
149+
panic!("Indices {ai:?}, {bi:?} and {ci:?} are not disjoint!")
150+
}
151+
Err(IndexOutOfBounds) => {
152+
panic!("Some indices among ({ai:?}, {bi:?}, {ci:?}) are out of bounds")
153+
}
154+
}
150155
}
151156

152157
#[inline]

0 commit comments

Comments
 (0)