about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTrevor Gross <tmgross@umich.edu>2023-11-03 19:59:26 -0500
committerTrevor Gross <tmgross@umich.edu>2023-11-03 20:44:21 -0500
commit55020c6f2f4aeac24e24d9bf4a623415c83c4608 (patch)
treedcfe360da6aa176166e536e46f521a13bd9f5657
parent9c20ddd956426d577d77cb3f57a7db2227a3c6e9 (diff)
downloadrust-55020c6f2f4aeac24e24d9bf4a623415c83c4608.tar.gz
rust-55020c6f2f4aeac24e24d9bf4a623415c83c4608.zip
Reverse ordering of `split_{first,last}_chunk` to be `(preceding, last)`
These methods currently return `(last_chunk, preceding_slice)`, which matches
the existing `split_x` methods that remove one item.

Change these to instead return `(preceding_slice, last_chunk)` which matches
string split methods, should be more intuitive, and will allow for consistency
with methods that split more items.
-rw-r--r--library/core/src/slice/mod.rs14
1 files changed, 7 insertions, 7 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
index 45080eda2ce..970b1cb4f1d 100644
--- a/library/core/src/slice/mod.rs
+++ b/library/core/src/slice/mod.rs
@@ -456,15 +456,15 @@ impl<T> [T] {
     ///
     /// let x = &[0, 1, 2];
     ///
-    /// if let Some((last, elements)) = x.split_last_chunk::<2>() {
-    ///     assert_eq!(last, &[1, 2]);
+    /// if let Some((elements, last)) = x.split_last_chunk::<2>() {
     ///     assert_eq!(elements, &[0]);
+    ///     assert_eq!(last, &[1, 2]);
     /// }
     /// ```
     #[unstable(feature = "slice_first_last_chunk", issue = "111774")]
     #[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")]
     #[inline]
-    pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])> {
+    pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T], &[T; N])> {
         if self.len() < N {
             None
         } else {
@@ -473,7 +473,7 @@ impl<T> [T] {
 
             // SAFETY: We explicitly check for the correct number of elements,
             //   and do not let the references outlive the slice.
-            Some((unsafe { &*(last.as_ptr() as *const [T; N]) }, init))
+            Some((init, unsafe { &*(last.as_ptr() as *const [T; N]) }))
         }
     }
 
@@ -486,7 +486,7 @@ impl<T> [T] {
     ///
     /// let x = &mut [0, 1, 2];
     ///
-    /// if let Some((last, elements)) = x.split_last_chunk_mut::<2>() {
+    /// if let Some((elements, last)) = x.split_last_chunk_mut::<2>() {
     ///     last[0] = 3;
     ///     last[1] = 4;
     ///     elements[0] = 5;
@@ -498,7 +498,7 @@ impl<T> [T] {
     #[inline]
     pub const fn split_last_chunk_mut<const N: usize>(
         &mut self,
-    ) -> Option<(&mut [T; N], &mut [T])> {
+    ) -> Option<(&mut [T], &mut [T; N])> {
         if self.len() < N {
             None
         } else {
@@ -508,7 +508,7 @@ impl<T> [T] {
             // SAFETY: We explicitly check for the correct number of elements,
             //   do not let the reference outlive the slice,
             //   and enforce exclusive mutability of the chunk by the split.
-            Some((unsafe { &mut *(last.as_mut_ptr() as *mut [T; N]) }, init))
+            Some((init, unsafe { &mut *(last.as_mut_ptr() as *mut [T; N]) }))
         }
     }