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
This commit is contained in:
@@ -4445,8 +4445,10 @@ impl<T> [T] {
|
|||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
|
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
|
||||||
pub fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
|
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
|
||||||
let (first, rem) = self.split_first()?;
|
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;
|
*self = rem;
|
||||||
Some(first)
|
Some(first)
|
||||||
}
|
}
|
||||||
@@ -4468,8 +4470,11 @@ impl<T> [T] {
|
|||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
|
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
|
||||||
pub fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
|
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
|
||||||
let (first, rem) = mem::take(self).split_first_mut()?;
|
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;
|
*self = rem;
|
||||||
Some(first)
|
Some(first)
|
||||||
}
|
}
|
||||||
@@ -4490,8 +4495,10 @@ impl<T> [T] {
|
|||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
|
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
|
||||||
pub fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
|
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
|
||||||
let (last, rem) = self.split_last()?;
|
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;
|
*self = rem;
|
||||||
Some(last)
|
Some(last)
|
||||||
}
|
}
|
||||||
@@ -4513,8 +4520,11 @@ impl<T> [T] {
|
|||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
|
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
|
||||||
pub fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
|
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
|
||||||
let (last, rem) = mem::take(self).split_last_mut()?;
|
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;
|
*self = rem;
|
||||||
Some(last)
|
Some(last)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user