|
1 | 1 | use std::fmt; |
2 | 2 | use std::marker::PhantomData; |
3 | 3 | use std::ops::{Index,IndexMut}; |
| 4 | +use std::slice::GetDisjointMutError::*; |
4 | 5 | use std::slice::{self,SliceIndex}; |
5 | 6 |
|
6 | 7 | usecrate::{Idx,IndexVec,IntoSliceIdx}; |
@@ -121,32 +122,36 @@ impl<I: Idx, T> IndexSlice<I, T> { |
121 | 122 |
|
122 | 123 | /// Returns mutable references to two distinct elements, `a` and `b`. |
123 | 124 | /// |
124 | | -/// Panics if `a == b`. |
| 125 | +/// Panics if `a == b` or if some of them are out of bounds. |
125 | 126 | #[inline] |
126 | 127 | pubfnpick2_mut(&mutself,a:I,b:I) -> (&mutT,&mutT){ |
127 | 128 | 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 | +} |
136 | 136 | } |
137 | 137 | } |
138 | 138 |
|
139 | 139 | /// Returns mutable references to three distinct elements. |
140 | 140 | /// |
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. |
142 | 142 | #[inline] |
143 | 143 | pubfnpick3_mut(&mutself,a:I,b:I,c:I) -> (&mutT,&mutT,&mutT){ |
144 | 144 | 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 | +} |
150 | 155 | } |
151 | 156 |
|
152 | 157 | #[inline] |
|
0 commit comments