about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-03-19 16:52:55 +0100
committerGitHub <noreply@github.com>2025-03-19 16:52:55 +0100
commit2df731d586319ec9a90676fcf7eca43ca60825c5 (patch)
tree343f6ddb385a40265b3072c48c902c004a193046
parent2ab69b898a96a81767ff6be84c5cc26d144d202f (diff)
parente1388bfb036d1511587dd75e917038b9080a43d1 (diff)
downloadrust-2df731d586319ec9a90676fcf7eca43ca60825c5.tar.gz
rust-2df731d586319ec9a90676fcf7eca43ca60825c5.zip
Rollup merge of #138540 - okaneco:const_split_off_first_last, r=m-ou-se
core/slice: Mark some `split_off` variants unstably const

Tracking issue: #138539

Add feature gate `#![feature(const_split_off_first_last)]`
Mark `split_off_first`, `split_off_first_mut`, `split_off_last`, and `split_off_last_mut` slice methods unstably const
-rw-r--r--library/core/src/slice/mod.rs26
1 files changed, 18 insertions, 8 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
index 2c699ee9fdd..5bb7243c449 100644
--- a/library/core/src/slice/mod.rs
+++ b/library/core/src/slice/mod.rs
@@ -4445,8 +4445,10 @@ impl<T> [T] {
     /// ```
     #[inline]
     #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
-    pub fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
-        let (first, rem) = self.split_first()?;
+    #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
+    pub const fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
+        // FIXME(const-hack): Use `?` when available in const instead of `let-else`.
+        let Some((first, rem)) = self.split_first() else { return None };
         *self = rem;
         Some(first)
     }
@@ -4468,8 +4470,11 @@ impl<T> [T] {
     /// ```
     #[inline]
     #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
-    pub fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
-        let (first, rem) = mem::take(self).split_first_mut()?;
+    #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
+    pub const fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
+        // FIXME(const-hack): Use `mem::take` and `?` when available in const.
+        // Original: `mem::take(self).split_first_mut()?`
+        let Some((first, rem)) = mem::replace(self, &mut []).split_first_mut() else { return None };
         *self = rem;
         Some(first)
     }
@@ -4490,8 +4495,10 @@ impl<T> [T] {
     /// ```
     #[inline]
     #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
-    pub fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
-        let (last, rem) = self.split_last()?;
+    #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
+    pub const fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
+        // FIXME(const-hack): Use `?` when available in const instead of `let-else`.
+        let Some((last, rem)) = self.split_last() else { return None };
         *self = rem;
         Some(last)
     }
@@ -4513,8 +4520,11 @@ impl<T> [T] {
     /// ```
     #[inline]
     #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
-    pub fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
-        let (last, rem) = mem::take(self).split_last_mut()?;
+    #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
+    pub const fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
+        // FIXME(const-hack): Use `mem::take` and `?` when available in const.
+        // Original: `mem::take(self).split_last_mut()?`
+        let Some((last, rem)) = mem::replace(self, &mut []).split_last_mut() else { return None };
         *self = rem;
         Some(last)
     }