Skip to content

Commit

Permalink
Remove {,r}split_array_ref{,_mut} methods from slices
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tgross35 committed Nov 30, 2023
1 parent 6e8ec85 commit 01337bf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 200 deletions.
8 changes: 4 additions & 4 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ impl<T, const N: usize> [T; N] {
)]
#[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.
Expand Down Expand Up @@ -680,7 +680,7 @@ impl<T, const N: usize> [T; N] {
)]
#[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.
Expand Down Expand Up @@ -725,7 +725,7 @@ impl<T, const N: usize> [T; N] {
)]
#[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.
Expand Down Expand Up @@ -758,7 +758,7 @@ impl<T, const N: usize> [T; N] {
)]
#[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()
}
}

Expand Down
170 changes: 12 additions & 158 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ impl<T> [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")]
Expand Down Expand Up @@ -397,6 +399,8 @@ impl<T> [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")]
Expand Down Expand Up @@ -432,6 +436,8 @@ impl<T> [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")]
Expand Down Expand Up @@ -467,6 +473,8 @@ impl<T> [T] {
/// 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")]
Expand Down Expand Up @@ -502,6 +510,8 @@ impl<T> [T] {
/// 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")]
Expand Down Expand Up @@ -573,6 +583,8 @@ impl<T> [T] {
/// 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")]
Expand Down Expand Up @@ -2035,164 +2047,6 @@ impl<T> [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.
///
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
52 changes: 14 additions & 38 deletions library/core/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2399,37 +2399,45 @@ mod swap_panics {
}

#[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]
Expand All @@ -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][..];
Expand Down

0 comments on commit 01337bf

Please sign in to comment.