Skip to content

Commit

Permalink
Stabilize const_slice_split_at_mut and const_slice_first_last_chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
onestacked committed Sep 29, 2024
1 parent 42ff2ee commit 949a4da
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
1 change: 0 additions & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
#![feature(const_size_of_val_raw)]
#![feature(const_slice_from_raw_parts_mut)]
#![feature(const_slice_from_ref)]
#![feature(const_slice_split_at_mut)]
#![feature(const_str_as_mut)]
#![feature(const_str_from_utf8_unchecked_mut)]
#![feature(const_strict_overflow_ops)]
Expand Down
91 changes: 73 additions & 18 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ impl<T> [T] {
#[inline]
#[must_use]
pub const fn first(&self) -> Option<&T> {
if let [first, ..] = self { Some(first) } else { None }
if let [first, ..] = self {
Some(first)
} else {
None
}
}

/// Returns a mutable reference to the first element of the slice, or `None` if it is empty.
Expand All @@ -176,7 +180,11 @@ impl<T> [T] {
#[inline]
#[must_use]
pub const fn first_mut(&mut self) -> Option<&mut T> {
if let [first, ..] = self { Some(first) } else { None }
if let [first, ..] = self {
Some(first)
} else {
None
}
}

/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
Expand All @@ -196,7 +204,11 @@ impl<T> [T] {
#[inline]
#[must_use]
pub const fn split_first(&self) -> Option<(&T, &[T])> {
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
if let [first, tail @ ..] = self {
Some((first, tail))
} else {
None
}
}

/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
Expand All @@ -218,7 +230,11 @@ impl<T> [T] {
#[inline]
#[must_use]
pub const fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
if let [first, tail @ ..] = self {
Some((first, tail))
} else {
None
}
}

/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
Expand All @@ -238,7 +254,11 @@ impl<T> [T] {
#[inline]
#[must_use]
pub const fn split_last(&self) -> Option<(&T, &[T])> {
if let [init @ .., last] = self { Some((last, init)) } else { None }
if let [init @ .., last] = self {
Some((last, init))
} else {
None
}
}

/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
Expand All @@ -260,7 +280,11 @@ impl<T> [T] {
#[inline]
#[must_use]
pub const fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
if let [init @ .., last] = self { Some((last, init)) } else { None }
if let [init @ .., last] = self {
Some((last, init))
} else {
None
}
}

/// Returns the last element of the slice, or `None` if it is empty.
Expand All @@ -279,7 +303,11 @@ impl<T> [T] {
#[inline]
#[must_use]
pub const fn last(&self) -> Option<&T> {
if let [.., last] = self { Some(last) } else { None }
if let [.., last] = self {
Some(last)
} else {
None
}
}

/// Returns a mutable reference to the last item in the slice, or `None` if it is empty.
Expand All @@ -302,7 +330,11 @@ impl<T> [T] {
#[inline]
#[must_use]
pub const fn last_mut(&mut self) -> Option<&mut T> {
if let [.., last] = self { Some(last) } else { None }
if let [.., last] = self {
Some(last)
} else {
None
}
}

/// Returns an array reference to the first `N` items in the slice.
Expand Down Expand Up @@ -353,7 +385,8 @@ impl<T> [T] {
/// ```
#[inline]
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[rustc_const_unstable(feature = "const_slice_first_last_chunk", issue = "111774")]
#[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "CURRENT_RUSTC_VERSION")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn first_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]> {
if self.len() < N {
None
Expand Down Expand Up @@ -384,6 +417,7 @@ impl<T> [T] {
#[inline]
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[rustc_const_stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn split_first_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])> {
if self.len() < N {
None
Expand Down Expand Up @@ -418,7 +452,8 @@ impl<T> [T] {
/// ```
#[inline]
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[rustc_const_unstable(feature = "const_slice_first_last_chunk", issue = "111774")]
#[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "CURRENT_RUSTC_VERSION")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn split_first_chunk_mut<const N: usize>(
&mut self,
) -> Option<(&mut [T; N], &mut [T])> {
Expand Down Expand Up @@ -454,6 +489,7 @@ impl<T> [T] {
#[inline]
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[rustc_const_stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T], &[T; N])> {
if self.len() < N {
None
Expand Down Expand Up @@ -488,7 +524,8 @@ impl<T> [T] {
/// ```
#[inline]
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[rustc_const_unstable(feature = "const_slice_first_last_chunk", issue = "111774")]
#[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "CURRENT_RUSTC_VERSION")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn split_last_chunk_mut<const N: usize>(
&mut self,
) -> Option<(&mut [T], &mut [T; N])> {
Expand Down Expand Up @@ -524,6 +561,7 @@ impl<T> [T] {
#[inline]
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[rustc_const_stable(feature = "const_slice_last_chunk", since = "1.80.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn last_chunk<const N: usize>(&self) -> Option<&[T; N]> {
if self.len() < N {
None
Expand Down Expand Up @@ -557,7 +595,8 @@ impl<T> [T] {
/// ```
#[inline]
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
#[rustc_const_unstable(feature = "const_slice_first_last_chunk", issue = "111774")]
#[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "CURRENT_RUSTC_VERSION")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn last_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]> {
if self.len() < N {
None
Expand Down Expand Up @@ -1900,7 +1939,8 @@ impl<T> [T] {
#[inline]
#[track_caller]
#[must_use]
#[rustc_const_unstable(feature = "const_slice_split_at_mut", issue = "101804")]
#[rustc_const_stable(feature = "const_slice_split_at_mut", since = "CURRENT_RUSTC_VERSION")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
match self.split_at_mut_checked(mid) {
Some(pair) => pair,
Expand Down Expand Up @@ -2002,7 +2042,9 @@ impl<T> [T] {
/// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
/// ```
#[stable(feature = "slice_split_at_unchecked", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_slice_split_at_mut", issue = "101804")]
#[rustc_const_stable(feature = "const_slice_split_at_mut", since = "CURRENT_RUSTC_VERSION")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
#[rustc_allow_const_fn_unstable(const_slice_from_raw_parts_mut)]
#[inline]
#[must_use]
pub const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
Expand Down Expand Up @@ -2102,7 +2144,8 @@ impl<T> [T] {
/// assert_eq!(None, v.split_at_mut_checked(7));
/// ```
#[stable(feature = "split_at_checked", since = "1.80.0")]
#[rustc_const_unstable(feature = "const_slice_split_at_mut", issue = "101804")]
#[rustc_const_stable(feature = "const_slice_split_at_mut", since = "CURRENT_RUSTC_VERSION")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
#[inline]
#[must_use]
pub const fn split_at_mut_checked(&mut self, mid: usize) -> Option<(&mut [T], &mut [T])> {
Expand Down Expand Up @@ -3814,7 +3857,11 @@ impl<T> [T] {
//
// Luckily since all this is constant-evaluated... performance here matters not!
const fn gcd(a: usize, b: usize) -> usize {
if b == 0 { a } else { gcd(b, a % b) }
if b == 0 {
a
} else {
gcd(b, a % b)
}
}

// Explicitly wrap the function call in a const block so it gets
Expand Down Expand Up @@ -4587,7 +4634,11 @@ impl<T> [T] {

let offset = byte_offset / mem::size_of::<T>();

if offset < self.len() { Some(offset) } else { None }
if offset < self.len() {
Some(offset)
} else {
None
}
}

/// Returns the range of indices that a subslice points to.
Expand Down Expand Up @@ -4641,7 +4692,11 @@ impl<T> [T] {
let start = byte_start / core::mem::size_of::<T>();
let end = start.wrapping_add(subslice.len());

if start <= self.len() && end <= self.len() { Some(start..end) } else { None }
if start <= self.len() && end <= self.len() {
Some(start..end)
} else {
None
}
}
}

Expand Down

0 comments on commit 949a4da

Please sign in to comment.