Skip to content

Commit 532db8f

Browse files
authored
Rollup merge of rust-lang#100663 - clarfonthey:const-reverse, r=scottmcm
Make slice::reverse const I remember this not being doable for some reason before, but decided to try it again and everything worked out in the tests.
2 parents d0bcf15 + ae2b1db commit 532db8f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Diff for: library/core/src/slice/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,9 @@ impl<T> [T] {
674674
/// assert!(v == [3, 2, 1]);
675675
/// ```
676676
#[stable(feature = "rust1", since = "1.0.0")]
677+
#[rustc_const_unstable(feature = "const_reverse", issue = "100784")]
677678
#[inline]
678-
pub fn reverse(&mut self) {
679+
pub const fn reverse(&mut self) {
679680
let half_len = self.len() / 2;
680681
let Range { start, end } = self.as_mut_ptr_range();
681682

@@ -698,18 +699,20 @@ impl<T> [T] {
698699
revswap(front_half, back_half, half_len);
699700

700701
#[inline]
701-
fn revswap<T>(a: &mut [T], b: &mut [T], n: usize) {
702-
debug_assert_eq!(a.len(), n);
703-
debug_assert_eq!(b.len(), n);
702+
const fn revswap<T>(a: &mut [T], b: &mut [T], n: usize) {
703+
debug_assert!(a.len() == n);
704+
debug_assert!(b.len() == n);
704705

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

711-
for i in 0..n {
712+
let mut i = 0;
713+
while i < n {
712714
mem::swap(&mut a[i], &mut b[n - 1 - i]);
715+
i += 1;
713716
}
714717
}
715718
}

0 commit comments

Comments
 (0)