Skip to content

Commit e1388bf

Browse files
committed
core/slice: Mark some split_off variants unstably const
Introduce feature `const_split_off_first_last` Mark `split_off_first`, `split_off_first_mut`, `split_off_last`, and `split_off_last_mut` unstably const
1 parent 4d30011 commit e1388bf

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

library/core/src/slice/mod.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -4446,8 +4446,10 @@ impl<T> [T] {
44464446
/// ```
44474447
#[inline]
44484448
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
4449-
pub fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
4450-
let (first, rem) = self.split_first()?;
4449+
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
4450+
pub const fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
4451+
// FIXME(const-hack): Use `?` when available in const instead of `let-else`.
4452+
let Some((first, rem)) = self.split_first() else { return None };
44514453
*self = rem;
44524454
Some(first)
44534455
}
@@ -4469,8 +4471,11 @@ impl<T> [T] {
44694471
/// ```
44704472
#[inline]
44714473
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
4472-
pub fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
4473-
let (first, rem) = mem::take(self).split_first_mut()?;
4474+
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
4475+
pub const fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
4476+
// FIXME(const-hack): Use `mem::take` and `?` when available in const.
4477+
// Original: `mem::take(self).split_first_mut()?`
4478+
let Some((first, rem)) = mem::replace(self, &mut []).split_first_mut() else { return None };
44744479
*self = rem;
44754480
Some(first)
44764481
}
@@ -4491,8 +4496,10 @@ impl<T> [T] {
44914496
/// ```
44924497
#[inline]
44934498
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
4494-
pub fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
4495-
let (last, rem) = self.split_last()?;
4499+
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
4500+
pub const fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
4501+
// FIXME(const-hack): Use `?` when available in const instead of `let-else`.
4502+
let Some((last, rem)) = self.split_last() else { return None };
44964503
*self = rem;
44974504
Some(last)
44984505
}
@@ -4514,8 +4521,11 @@ impl<T> [T] {
45144521
/// ```
45154522
#[inline]
45164523
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
4517-
pub fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
4518-
let (last, rem) = mem::take(self).split_last_mut()?;
4524+
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
4525+
pub const fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
4526+
// FIXME(const-hack): Use `mem::take` and `?` when available in const.
4527+
// Original: `mem::take(self).split_last_mut()?`
4528+
let Some((last, rem)) = mem::replace(self, &mut []).split_last_mut() else { return None };
45194529
*self = rem;
45204530
Some(last)
45214531
}

0 commit comments

Comments
 (0)