|
1 | 1 | //! Indexing implementations for `[T]`. |
2 | 2 |
|
3 | | -use crate::intrinsics::assert_unsafe_precondition; |
4 | 3 | use crate::intrinsics::const_eval_select; |
5 | 4 | use crate::intrinsics::unchecked_sub; |
6 | 5 | use crate::ops; |
| 6 | +use crate::panic::debug_assert_nounwind; |
7 | 7 | use crate::ptr; |
8 | 8 |
|
9 | 9 | #[stable(feature = "rust1", since = "1.0.0")] |
@@ -225,31 +225,25 @@ unsafe impl<T> SliceIndex<[T]> for usize { |
225 | 225 |
|
226 | 226 | #[inline] |
227 | 227 | unsafe fn get_unchecked(self, slice: *const [T]) -> *const T { |
228 | | - let this = self; |
| 228 | + debug_assert_nounwind!( |
| 229 | + self < slice.len(), |
| 230 | + "slice::get_unchecked requires that the index is within the slice", |
| 231 | + ); |
229 | 232 | // SAFETY: the caller guarantees that `slice` is not dangling, so it |
230 | 233 | // cannot be longer than `isize::MAX`. They also guarantee that |
231 | 234 | // `self` is in bounds of `slice` so `self` cannot overflow an `isize`, |
232 | 235 | // so the call to `add` is safe. |
233 | | - unsafe { |
234 | | - assert_unsafe_precondition!( |
235 | | - "slice::get_unchecked requires that the index is within the slice", |
236 | | - [T](this: usize, slice: *const [T]) => this < slice.len() |
237 | | - ); |
238 | | - slice.as_ptr().add(self) |
239 | | - } |
| 236 | + unsafe { slice.as_ptr().add(self) } |
240 | 237 | } |
241 | 238 |
|
242 | 239 | #[inline] |
243 | 240 | unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T { |
244 | | - let this = self; |
| 241 | + debug_assert_nounwind!( |
| 242 | + self < slice.len(), |
| 243 | + "slice::get_unchecked_mut requires that the index is within the slice", |
| 244 | + ); |
245 | 245 | // SAFETY: see comments for `get_unchecked` above. |
246 | | - unsafe { |
247 | | - assert_unsafe_precondition!( |
248 | | - "slice::get_unchecked_mut requires that the index is within the slice", |
249 | | - [T](this: usize, slice: *mut [T]) => this < slice.len() |
250 | | - ); |
251 | | - slice.as_mut_ptr().add(self) |
252 | | - } |
| 246 | + unsafe { slice.as_mut_ptr().add(self) } |
253 | 247 | } |
254 | 248 |
|
255 | 249 | #[inline] |
@@ -293,32 +287,25 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange { |
293 | 287 |
|
294 | 288 | #[inline] |
295 | 289 | unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] { |
296 | | - let end = self.end(); |
| 290 | + debug_assert_nounwind!( |
| 291 | + self.end() <= slice.len(), |
| 292 | + "slice::get_unchecked requires that the index is within the slice" |
| 293 | + ); |
297 | 294 | // SAFETY: the caller guarantees that `slice` is not dangling, so it |
298 | 295 | // cannot be longer than `isize::MAX`. They also guarantee that |
299 | 296 | // `self` is in bounds of `slice` so `self` cannot overflow an `isize`, |
300 | 297 | // so the call to `add` is safe. |
301 | | - |
302 | | - unsafe { |
303 | | - assert_unsafe_precondition!( |
304 | | - "slice::get_unchecked requires that the index is within the slice", |
305 | | - [T](end: usize, slice: *const [T]) => end <= slice.len() |
306 | | - ); |
307 | | - ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len()) |
308 | | - } |
| 298 | + unsafe { ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len()) } |
309 | 299 | } |
310 | 300 |
|
311 | 301 | #[inline] |
312 | 302 | unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] { |
313 | | - let end = self.end(); |
| 303 | + debug_assert_nounwind!( |
| 304 | + self.end() <= slice.len(), |
| 305 | + "slice::get_unchecked_mut requires that the index is within the slice", |
| 306 | + ); |
314 | 307 | // SAFETY: see comments for `get_unchecked` above. |
315 | | - unsafe { |
316 | | - assert_unsafe_precondition!( |
317 | | - "slice::get_unchecked_mut requires that the index is within the slice", |
318 | | - [T](end: usize, slice: *mut [T]) => end <= slice.len() |
319 | | - ); |
320 | | - ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len()) |
321 | | - } |
| 308 | + unsafe { ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len()) } |
322 | 309 | } |
323 | 310 |
|
324 | 311 | #[inline] |
@@ -369,32 +356,28 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> { |
369 | 356 |
|
370 | 357 | #[inline] |
371 | 358 | unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] { |
372 | | - let this = ops::Range { ..self }; |
| 359 | + debug_assert_nounwind!( |
| 360 | + self.end >= self.start && self.end <= slice.len(), |
| 361 | + "slice::get_unchecked requires that the range is within the slice", |
| 362 | + ); |
373 | 363 | // SAFETY: the caller guarantees that `slice` is not dangling, so it |
374 | 364 | // cannot be longer than `isize::MAX`. They also guarantee that |
375 | 365 | // `self` is in bounds of `slice` so `self` cannot overflow an `isize`, |
376 | 366 | // so the call to `add` is safe and the length calculation cannot overflow. |
377 | 367 | unsafe { |
378 | | - assert_unsafe_precondition!( |
379 | | - "slice::get_unchecked requires that the range is within the slice", |
380 | | - [T](this: ops::Range<usize>, slice: *const [T]) => |
381 | | - this.end >= this.start && this.end <= slice.len() |
382 | | - ); |
383 | 368 | let new_len = unchecked_sub(self.end, self.start); |
384 | 369 | ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len) |
385 | 370 | } |
386 | 371 | } |
387 | 372 |
|
388 | 373 | #[inline] |
389 | 374 | unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] { |
390 | | - let this = ops::Range { ..self }; |
| 375 | + debug_assert_nounwind!( |
| 376 | + self.end >= self.start && self.end <= slice.len(), |
| 377 | + "slice::get_unchecked_mut requires that the range is within the slice", |
| 378 | + ); |
391 | 379 | // SAFETY: see comments for `get_unchecked` above. |
392 | 380 | unsafe { |
393 | | - assert_unsafe_precondition!( |
394 | | - "slice::get_unchecked_mut requires that the range is within the slice", |
395 | | - [T](this: ops::Range<usize>, slice: *mut [T]) => |
396 | | - this.end >= this.start && this.end <= slice.len() |
397 | | - ); |
398 | 381 | let new_len = unchecked_sub(self.end, self.start); |
399 | 382 | ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len) |
400 | 383 | } |
|
0 commit comments