Remove {,r}split_array_ref{,_mut} methods from slices

The functionality of these methods from `split_array` has been absorbed by the
`slice_first_last_chunk` feature. This only affects the methods on slices,
not those with the same name that are implemented on array types.

Also adjusts testing to reflect this change.
This commit is contained in:
Trevor Gross 2023-11-03 20:38:30 -05:00
parent 6e8ec852f7
commit 01337bf1fd
4 changed files with 31 additions and 200 deletions

View File

@ -647,7 +647,7 @@ pub fn as_mut_slice(&mut self) -> &mut [T] {
)]
#[inline]
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T]) {
(&self[..]).split_array_ref::<M>()
(&self[..]).split_first_chunk::<M>().unwrap()
}
/// Divides one mutable array reference into two at an index.
@ -680,7 +680,7 @@ pub fn as_mut_slice(&mut self) -> &mut [T] {
)]
#[inline]
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T]) {
(&mut self[..]).split_array_mut::<M>()
(&mut self[..]).split_first_chunk_mut::<M>().unwrap()
}
/// Divides one array reference into two at an index from the end.
@ -725,7 +725,7 @@ pub fn as_mut_slice(&mut self) -> &mut [T] {
)]
#[inline]
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M]) {
(&self[..]).rsplit_array_ref::<M>()
(&self[..]).split_last_chunk::<M>().unwrap()
}
/// Divides one mutable array reference into two at an index from the end.
@ -758,7 +758,7 @@ pub fn as_mut_slice(&mut self) -> &mut [T] {
)]
#[inline]
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M]) {
(&mut self[..]).rsplit_array_mut::<M>()
(&mut self[..]).split_last_chunk_mut::<M>().unwrap()
}
}

View File

@ -367,6 +367,8 @@ pub const fn last_mut(&mut self) -> Option<&mut T> {
/// first[1] = 4;
/// }
/// assert_eq!(x, &[5, 4, 2]);
///
/// assert_eq!(None, x.first_chunk_mut::<4>());
/// ```
#[unstable(feature = "slice_first_last_chunk", issue = "111774")]
#[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")]
@ -397,6 +399,8 @@ pub const fn last_mut(&mut self) -> Option<&mut T> {
/// assert_eq!(first, &[0, 1]);
/// assert_eq!(elements, &[2]);
/// }
///
/// assert_eq!(None, x.split_first_chunk::<4>());
/// ```
#[unstable(feature = "slice_first_last_chunk", issue = "111774")]
#[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")]
@ -432,6 +436,8 @@ pub const fn last_mut(&mut self) -> Option<&mut T> {
/// elements[0] = 5;
/// }
/// assert_eq!(x, &[3, 4, 5]);
///
/// assert_eq!(None, x.split_first_chunk_mut::<4>());
/// ```
#[unstable(feature = "slice_first_last_chunk", issue = "111774")]
#[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")]
@ -467,6 +473,8 @@ pub const fn split_first_chunk_mut<const N: usize>(
/// assert_eq!(elements, &[0]);
/// assert_eq!(last, &[1, 2]);
/// }
///
/// assert_eq!(None, x.split_last_chunk::<4>());
/// ```
#[unstable(feature = "slice_first_last_chunk", issue = "111774")]
#[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")]
@ -502,6 +510,8 @@ pub const fn split_first_chunk_mut<const N: usize>(
/// elements[0] = 5;
/// }
/// assert_eq!(x, &[5, 3, 4]);
///
/// assert_eq!(None, x.split_last_chunk_mut::<4>());
/// ```
#[unstable(feature = "slice_first_last_chunk", issue = "111774")]
#[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")]
@ -573,6 +583,8 @@ pub const fn split_last_chunk_mut<const N: usize>(
/// last[1] = 20;
/// }
/// assert_eq!(x, &[0, 10, 20]);
///
/// assert_eq!(None, x.last_chunk_mut::<4>());
/// ```
#[unstable(feature = "slice_first_last_chunk", issue = "111774")]
#[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")]
@ -2035,164 +2047,6 @@ pub const fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
}
}
/// Divides one slice into an array and a remainder slice at an index.
///
/// The array will contain all indices from `[0, N)` (excluding
/// the index `N` itself) and the slice will contain all
/// indices from `[N, len)` (excluding the index `len` itself).
///
/// # Panics
///
/// Panics if `N > len`.
///
/// # Examples
///
/// ```
/// #![feature(split_array)]
///
/// let v = &[1, 2, 3, 4, 5, 6][..];
///
/// {
/// let (left, right) = v.split_array_ref::<0>();
/// assert_eq!(left, &[]);
/// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
/// }
///
/// {
/// let (left, right) = v.split_array_ref::<2>();
/// assert_eq!(left, &[1, 2]);
/// assert_eq!(right, [3, 4, 5, 6]);
/// }
///
/// {
/// let (left, right) = v.split_array_ref::<6>();
/// assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
/// assert_eq!(right, []);
/// }
/// ```
#[unstable(feature = "split_array", reason = "new API", issue = "90091")]
#[inline]
#[track_caller]
#[must_use]
pub fn split_array_ref<const N: usize>(&self) -> (&[T; N], &[T]) {
let (a, b) = self.split_at(N);
// SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
unsafe { (&*(a.as_ptr() as *const [T; N]), b) }
}
/// Divides one mutable slice into an array and a remainder slice at an index.
///
/// The array will contain all indices from `[0, N)` (excluding
/// the index `N` itself) and the slice will contain all
/// indices from `[N, len)` (excluding the index `len` itself).
///
/// # Panics
///
/// Panics if `N > len`.
///
/// # Examples
///
/// ```
/// #![feature(split_array)]
///
/// let mut v = &mut [1, 0, 3, 0, 5, 6][..];
/// let (left, right) = v.split_array_mut::<2>();
/// assert_eq!(left, &mut [1, 0]);
/// assert_eq!(right, [3, 0, 5, 6]);
/// left[1] = 2;
/// right[1] = 4;
/// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
/// ```
#[unstable(feature = "split_array", reason = "new API", issue = "90091")]
#[inline]
#[track_caller]
#[must_use]
pub fn split_array_mut<const N: usize>(&mut self) -> (&mut [T; N], &mut [T]) {
let (a, b) = self.split_at_mut(N);
// SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
unsafe { (&mut *(a.as_mut_ptr() as *mut [T; N]), b) }
}
/// Divides one slice into an array and a remainder slice at an index from
/// the end.
///
/// The slice will contain all indices from `[0, len - N)` (excluding
/// the index `len - N` itself) and the array will contain all
/// indices from `[len - N, len)` (excluding the index `len` itself).
///
/// # Panics
///
/// Panics if `N > len`.
///
/// # Examples
///
/// ```
/// #![feature(split_array)]
///
/// let v = &[1, 2, 3, 4, 5, 6][..];
///
/// {
/// let (left, right) = v.rsplit_array_ref::<0>();
/// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
/// assert_eq!(right, &[]);
/// }
///
/// {
/// let (left, right) = v.rsplit_array_ref::<2>();
/// assert_eq!(left, [1, 2, 3, 4]);
/// assert_eq!(right, &[5, 6]);
/// }
///
/// {
/// let (left, right) = v.rsplit_array_ref::<6>();
/// assert_eq!(left, []);
/// assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
/// }
/// ```
#[unstable(feature = "split_array", reason = "new API", issue = "90091")]
#[inline]
#[must_use]
pub fn rsplit_array_ref<const N: usize>(&self) -> (&[T], &[T; N]) {
assert!(N <= self.len());
let (a, b) = self.split_at(self.len() - N);
// SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at)
unsafe { (a, &*(b.as_ptr() as *const [T; N])) }
}
/// Divides one mutable slice into an array and a remainder slice at an
/// index from the end.
///
/// The slice will contain all indices from `[0, len - N)` (excluding
/// the index `N` itself) and the array will contain all
/// indices from `[len - N, len)` (excluding the index `len` itself).
///
/// # Panics
///
/// Panics if `N > len`.
///
/// # Examples
///
/// ```
/// #![feature(split_array)]
///
/// let mut v = &mut [1, 0, 3, 0, 5, 6][..];
/// let (left, right) = v.rsplit_array_mut::<4>();
/// assert_eq!(left, [1, 0]);
/// assert_eq!(right, &mut [3, 0, 5, 6]);
/// left[1] = 2;
/// right[1] = 4;
/// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
/// ```
#[unstable(feature = "split_array", reason = "new API", issue = "90091")]
#[inline]
#[must_use]
pub fn rsplit_array_mut<const N: usize>(&mut self) -> (&mut [T], &mut [T; N]) {
assert!(N <= self.len());
let (a, b) = self.split_at_mut(self.len() - N);
// SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
unsafe { (a, &mut *(b.as_mut_ptr() as *mut [T; N])) }
}
/// Returns an iterator over subslices separated by elements that match
/// `pred`. The matched element is not contained in the subslices.
///

View File

@ -46,6 +46,7 @@
#![feature(pattern)]
#![feature(sort_internals)]
#![feature(slice_take)]
#![feature(slice_first_last_chunk)]
#![feature(slice_from_ptr_range)]
#![feature(slice_split_once)]
#![feature(split_as_slice)]

View File

@ -2399,37 +2399,45 @@ fn index_b_greater_than_len() {
}
#[test]
fn slice_split_array_mut() {
fn slice_split_first_chunk_mut() {
let v = &mut [1, 2, 3, 4, 5, 6][..];
{
let (left, right) = v.split_array_mut::<0>();
let (left, right) = v.split_first_chunk_mut::<0>().unwrap();
assert_eq!(left, &mut []);
assert_eq!(right, [1, 2, 3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_mut::<6>();
let (left, right) = v.split_first_chunk_mut::<6>().unwrap();
assert_eq!(left, &mut [1, 2, 3, 4, 5, 6]);
assert_eq!(right, []);
}
{
assert!(v.split_first_chunk_mut::<7>().is_none());
}
}
#[test]
fn slice_rsplit_array_mut() {
fn slice_split_last_chunk_mut() {
let v = &mut [1, 2, 3, 4, 5, 6][..];
{
let (left, right) = v.rsplit_array_mut::<0>();
let (left, right) = v.split_last_chunk_mut::<0>().unwrap();
assert_eq!(left, [1, 2, 3, 4, 5, 6]);
assert_eq!(right, &mut []);
}
{
let (left, right) = v.rsplit_array_mut::<6>();
let (left, right) = v.split_last_chunk_mut::<6>().unwrap();
assert_eq!(left, []);
assert_eq!(right, &mut [1, 2, 3, 4, 5, 6]);
}
{
assert!(v.split_last_chunk_mut::<7>().is_none());
}
}
#[test]
@ -2444,38 +2452,6 @@ fn split_as_slice() {
assert_eq!(split.as_slice(), &[]);
}
#[should_panic]
#[test]
fn slice_split_array_ref_out_of_bounds() {
let v = &[1, 2, 3, 4, 5, 6][..];
let _ = v.split_array_ref::<7>();
}
#[should_panic]
#[test]
fn slice_split_array_mut_out_of_bounds() {
let v = &mut [1, 2, 3, 4, 5, 6][..];
let _ = v.split_array_mut::<7>();
}
#[should_panic]
#[test]
fn slice_rsplit_array_ref_out_of_bounds() {
let v = &[1, 2, 3, 4, 5, 6][..];
let _ = v.rsplit_array_ref::<7>();
}
#[should_panic]
#[test]
fn slice_rsplit_array_mut_out_of_bounds() {
let v = &mut [1, 2, 3, 4, 5, 6][..];
let _ = v.rsplit_array_mut::<7>();
}
#[test]
fn slice_split_once() {
let v = &[1, 2, 3, 2, 4][..];