|
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 | use crate::{Idx, IndexVec, IntoSliceIdx}; |
@@ -115,33 +116,35 @@ impl<I: Idx, T> IndexSlice<I, T> { |
115 | 116 |
|
116 | 117 | /// Returns mutable references to two distinct elements, `a` and `b`. |
117 | 118 | /// |
118 | | - /// Panics if `a == b`. |
| 119 | + /// Panics if `a == b` or if some of them are out of bounds. |
119 | 120 | #[inline] |
120 | 121 | pub fn pick2_mut(&mut self, a: I, b: I) -> (&mut T, &mut T) { |
121 | 122 | let (ai, bi) = (a.index(), b.index()); |
122 | | - assert!(ai != bi); |
123 | | - let len = self.raw.len(); |
124 | | - assert!(ai < len && bi < len); |
125 | 123 |
|
126 | 124 | match self.raw.get_disjoint_mut([ai, bi]) { |
127 | 125 | Ok([a, b]) => (a, b), |
128 | | - Err(_) => panic!("Can't get mutable references"), |
| 126 | + Err(OverlappingIndices) => panic!("Indices {ai:?} and {bi:?} are not disjoint!"), |
| 127 | + Err(IndexOutOfBounds) => { |
| 128 | + panic!("Some indices among ({ai:?}, {bi:?}) are out of bounds") |
| 129 | + } |
129 | 130 | } |
130 | 131 | } |
131 | 132 |
|
132 | 133 | /// Returns mutable references to three distinct elements. |
133 | 134 | /// |
134 | | - /// Panics if the elements are not distinct. |
| 135 | + /// Panics if the elements are not distinct or if some of them are out of bounds. |
135 | 136 | #[inline] |
136 | 137 | pub fn pick3_mut(&mut self, a: I, b: I, c: I) -> (&mut T, &mut T, &mut T) { |
137 | 138 | let (ai, bi, ci) = (a.index(), b.index(), c.index()); |
138 | | - assert!(ai != bi && bi != ci && ci != ai); |
139 | | - let len = self.raw.len(); |
140 | | - assert!(ai < len && bi < len && ci < len); |
141 | 139 |
|
142 | 140 | match self.raw.get_disjoint_mut([ai, bi, ci]) { |
143 | 141 | Ok([a, b, c]) => (a, b, c), |
144 | | - Err(_) => panic!("Can't get mutable references"), |
| 142 | + Err(OverlappingIndices) => { |
| 143 | + panic!("Indices {ai:?}, {bi:?} and {ci:?} are not disjoint!") |
| 144 | + } |
| 145 | + Err(IndexOutOfBounds) => { |
| 146 | + panic!("Some indices among ({ai:?}, {bi:?}, {ci:?}) are out of bounds") |
| 147 | + } |
145 | 148 | } |
146 | 149 | } |
147 | 150 |
|
|
0 commit comments