|
8 | 8 |
|
9 | 9 | use crate::cmp::Ordering::{self, Equal, Greater, Less};
|
10 | 10 | use crate::fmt;
|
11 |
| -use crate::intrinsics::{assert_unsafe_precondition, exact_div}; |
| 11 | +use crate::intrinsics::exact_div; |
12 | 12 | use crate::marker::Copy;
|
13 | 13 | use crate::mem::{self, SizedTypeProperties};
|
14 | 14 | use crate::num::NonZeroUsize;
|
15 | 15 | use crate::ops::{Bound, FnMut, OneSidedRange, Range, RangeBounds};
|
16 | 16 | use crate::option::Option;
|
17 | 17 | use crate::option::Option::{None, Some};
|
| 18 | +use crate::panic::debug_assert_nounwind; |
18 | 19 | use crate::ptr;
|
19 | 20 | use crate::result::Result;
|
20 | 21 | use crate::result::Result::{Err, Ok};
|
@@ -929,14 +930,14 @@ impl<T> [T] {
|
929 | 930 | #[unstable(feature = "slice_swap_unchecked", issue = "88539")]
|
930 | 931 | #[rustc_const_unstable(feature = "const_swap", issue = "83163")]
|
931 | 932 | pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
|
932 |
| - let this = self; |
933 |
| - let ptr = this.as_mut_ptr(); |
| 933 | + debug_assert_nounwind!( |
| 934 | + a < self.len() && b < self.len(), |
| 935 | + "slice::swap_unchecked requires that the indices are within the slice", |
| 936 | + ); |
| 937 | + |
| 938 | + let ptr = self.as_mut_ptr(); |
934 | 939 | // SAFETY: caller has to guarantee that `a < self.len()` and `b < self.len()`
|
935 | 940 | unsafe {
|
936 |
| - assert_unsafe_precondition!( |
937 |
| - "slice::swap_unchecked requires that the indices are within the slice", |
938 |
| - [T](a: usize, b: usize, this: &mut [T]) => a < this.len() && b < this.len() |
939 |
| - ); |
940 | 941 | ptr::swap(ptr.add(a), ptr.add(b));
|
941 | 942 | }
|
942 | 943 | }
|
@@ -1269,15 +1270,12 @@ impl<T> [T] {
|
1269 | 1270 | #[inline]
|
1270 | 1271 | #[must_use]
|
1271 | 1272 | pub const unsafe fn as_chunks_unchecked<const N: usize>(&self) -> &[[T; N]] {
|
1272 |
| - let this = self; |
| 1273 | + debug_assert_nounwind!( |
| 1274 | + N != 0 && self.len() % N == 0, |
| 1275 | + "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks", |
| 1276 | + ); |
1273 | 1277 | // SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
|
1274 |
| - let new_len = unsafe { |
1275 |
| - assert_unsafe_precondition!( |
1276 |
| - "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks", |
1277 |
| - [T](this: &[T], N: usize) => N != 0 && this.len() % N == 0 |
1278 |
| - ); |
1279 |
| - exact_div(self.len(), N) |
1280 |
| - }; |
| 1278 | + let new_len = unsafe { exact_div(self.len(), N) }; |
1281 | 1279 | // SAFETY: We cast a slice of `new_len * N` elements into
|
1282 | 1280 | // a slice of `new_len` many `N` elements chunks.
|
1283 | 1281 | unsafe { from_raw_parts(self.as_ptr().cast(), new_len) }
|
@@ -1426,15 +1424,12 @@ impl<T> [T] {
|
1426 | 1424 | #[inline]
|
1427 | 1425 | #[must_use]
|
1428 | 1426 | pub const unsafe fn as_chunks_unchecked_mut<const N: usize>(&mut self) -> &mut [[T; N]] {
|
1429 |
| - let this = &*self; |
| 1427 | + debug_assert_nounwind!( |
| 1428 | + N != 0 && self.len() % N == 0, |
| 1429 | + "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks", |
| 1430 | + ); |
1430 | 1431 | // SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
|
1431 |
| - let new_len = unsafe { |
1432 |
| - assert_unsafe_precondition!( |
1433 |
| - "slice::as_chunks_unchecked_mut requires `N != 0` and the slice to split exactly into `N`-element chunks", |
1434 |
| - [T](this: &[T], N: usize) => N != 0 && this.len() % N == 0 |
1435 |
| - ); |
1436 |
| - exact_div(this.len(), N) |
1437 |
| - }; |
| 1432 | + let new_len = unsafe { exact_div(self.len(), N) }; |
1438 | 1433 | // SAFETY: We cast a slice of `new_len * N` elements into
|
1439 | 1434 | // a slice of `new_len` many `N` elements chunks.
|
1440 | 1435 | unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) }
|
@@ -1967,14 +1962,13 @@ impl<T> [T] {
|
1967 | 1962 | let len = self.len();
|
1968 | 1963 | let ptr = self.as_ptr();
|
1969 | 1964 |
|
| 1965 | + debug_assert_nounwind!( |
| 1966 | + mid <= len, |
| 1967 | + "slice::split_at_unchecked requires the index to be within the slice", |
| 1968 | + ); |
| 1969 | + |
1970 | 1970 | // SAFETY: Caller has to check that `0 <= mid <= self.len()`
|
1971 |
| - unsafe { |
1972 |
| - assert_unsafe_precondition!( |
1973 |
| - "slice::split_at_unchecked requires the index to be within the slice", |
1974 |
| - (mid: usize, len: usize) => mid <= len |
1975 |
| - ); |
1976 |
| - (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) |
1977 |
| - } |
| 1971 | + unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) } |
1978 | 1972 | }
|
1979 | 1973 |
|
1980 | 1974 | /// Divides one mutable slice into two at an index, without doing bounds checking.
|
@@ -2018,17 +2012,16 @@ impl<T> [T] {
|
2018 | 2012 | let len = self.len();
|
2019 | 2013 | let ptr = self.as_mut_ptr();
|
2020 | 2014 |
|
| 2015 | + debug_assert_nounwind!( |
| 2016 | + mid <= len, |
| 2017 | + "slice::split_at_mut_unchecked requires the index to be within the slice", |
| 2018 | + ); |
| 2019 | + |
2021 | 2020 | // SAFETY: Caller has to check that `0 <= mid <= self.len()`.
|
2022 | 2021 | //
|
2023 | 2022 | // `[ptr; mid]` and `[mid; len]` are not overlapping, so returning a mutable reference
|
2024 | 2023 | // is fine.
|
2025 |
| - unsafe { |
2026 |
| - assert_unsafe_precondition!( |
2027 |
| - "slice::split_at_mut_unchecked requires the index to be within the slice", |
2028 |
| - (mid: usize, len: usize) => mid <= len |
2029 |
| - ); |
2030 |
| - (from_raw_parts_mut(ptr, mid), from_raw_parts_mut(ptr.add(mid), len - mid)) |
2031 |
| - } |
| 2024 | + unsafe { (from_raw_parts_mut(ptr, mid), from_raw_parts_mut(ptr.add(mid), len - mid)) } |
2032 | 2025 | }
|
2033 | 2026 |
|
2034 | 2027 | /// Divides one slice into an array and a remainder slice at an index.
|
|
0 commit comments