about summary refs log tree commit diff
path: root/library/core/src/array
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-10-23 06:13:18 +0000
committerbors <bors@rust-lang.org>2021-10-23 06:13:18 +0000
commitcf708558b758f4473c4f35986d9492ace7bf906d (patch)
tree2f4b1fb7cff6c2182d10cc4903ed7826f9a72e06 /library/core/src/array
parenta3f7c4db0373aa077f86cdd1bf11122845d3b65a (diff)
parenta05a1294d08e285f7039293298d9170fb3117013 (diff)
downloadrust-cf708558b758f4473c4f35986d9492ace7bf906d.tar.gz
rust-cf708558b758f4473c4f35986d9492ace7bf906d.zip
Auto merge of #90188 - matthiaskrgr:rollup-74cwv5c, r=matthiaskrgr
Rollup of 11 pull requests

Successful merges:

 - #83233 (Implement split_array and split_array_mut)
 - #88300 (Stabilise unix_process_wait_more, extra ExitStatusExt methods)
 - #89416 (nice_region_error: Include lifetime placeholders in error output)
 - #89468 (Report fatal lexer errors in `--cfg` command line arguments)
 - #89730 (add feature flag for `type_changing_struct_update`)
 - #89920 (Implement -Z location-detail flag)
 - #90070 (Add edition configuration to compiletest)
 - #90087 (Sync rustfmt subtree)
 - #90117 (Make RSplit<T, P>: Clone not require T: Clone)
 - #90122 (CI: make docker cache download and `docker load` time out after 10 minutes)
 - #90166 (Add comment documenting why we can't use a simpler solution)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/core/src/array')
-rw-r--r--library/core/src/array/mod.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs
index b27c36baf37..73340fda2cb 100644
--- a/library/core/src/array/mod.rs
+++ b/library/core/src/array/mod.rs
@@ -500,6 +500,84 @@ impl<T, const N: usize> [T; N] {
         // items.
         unsafe { collect_into_array_unchecked(&mut self.iter_mut()) }
     }
+
+    /// Divides one array reference into two at an index.
+    ///
+    /// The first will contain all indices from `[0, M)` (excluding
+    /// the index `M` itself) and the second will contain all
+    /// indices from `[M, N)` (excluding the index `N` itself).
+    ///
+    /// # Panics
+    ///
+    /// Panics if `M > N`.
+    ///
+    /// # 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 = "return type should have array as 2nd element",
+        issue = "90091"
+    )]
+    #[inline]
+    pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T]) {
+        (&self[..]).split_array_ref::<M>()
+    }
+
+    /// Divides one mutable array reference into two at an index.
+    ///
+    /// The first will contain all indices from `[0, M)` (excluding
+    /// the index `M` itself) and the second will contain all
+    /// indices from `[M, N)` (excluding the index `N` itself).
+    ///
+    /// # Panics
+    ///
+    /// Panics if `M > N`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(split_array)]
+    ///
+    /// let mut v = [1, 0, 3, 0, 5, 6];
+    /// let (left, right) = v.split_array_mut::<2>();
+    /// assert_eq!(left, &mut [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 = "return type should have array as 2nd element",
+        issue = "90091"
+    )]
+    #[inline]
+    pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T]) {
+        (&mut self[..]).split_array_mut::<M>()
+    }
 }
 
 /// Pulls `N` items from `iter` and returns them as an array. If the iterator