Skip to content

Commit

Permalink
Rollup merge of rust-lang#135121 - okaneco:const_slice_reverse, r=jhp…
Browse files Browse the repository at this point in the history
…ratt

Mark `slice::reverse` unstably const

Tracking issue rust-lang#135120

This is unblocked by the stabilization of `const_swap`
  • Loading branch information
jhpratt authored Jan 5, 2025
2 parents af9293f + 03c2ac2 commit 0f9f91c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,9 @@ impl<T> [T] {
/// assert!(v == [3, 2, 1]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_slice_reverse", issue = "135120")]
#[inline]
pub fn reverse(&mut self) {
pub const fn reverse(&mut self) {
let half_len = self.len() / 2;
let Range { start, end } = self.as_mut_ptr_range();

Expand All @@ -1011,15 +1012,16 @@ impl<T> [T] {
revswap(front_half, back_half, half_len);

#[inline]
fn revswap<T>(a: &mut [T], b: &mut [T], n: usize) {
const fn revswap<T>(a: &mut [T], b: &mut [T], n: usize) {
debug_assert!(a.len() == n);
debug_assert!(b.len() == n);

// Because this function is first compiled in isolation,
// this check tells LLVM that the indexing below is
// in-bounds. Then after inlining -- once the actual
// lengths of the slices are known -- it's removed.
let (a, b) = (&mut a[..n], &mut b[..n]);
let (a, _) = a.split_at_mut(n);
let (b, _) = b.split_at_mut(n);

let mut i = 0;
while i < n {
Expand Down

0 comments on commit 0f9f91c

Please sign in to comment.