diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-08-21 16:54:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-21 16:54:01 +0200 |
| commit | 1cdcf508bba11c91f48a3f7ca7870a6c1e575c6f (patch) | |
| tree | 97454dde51614746b1ec0ce1b66d2f4796a5bb4f | |
| parent | a5c16a5381cd71c4d27b32857d04d70345b12de4 (diff) | |
| parent | ae2b1dbc8915c2a41bc3555cceb65a07c0032b05 (diff) | |
| download | rust-1cdcf508bba11c91f48a3f7ca7870a6c1e575c6f.tar.gz rust-1cdcf508bba11c91f48a3f7ca7870a6c1e575c6f.zip | |
Rollup merge of #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.
| -rw-r--r-- | library/core/src/slice/mod.rs | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index e79d47c9f98..75c7c4d5581 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -674,8 +674,9 @@ impl<T> [T] { /// assert!(v == [3, 2, 1]); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_reverse", issue = "100784")] #[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(); @@ -698,9 +699,9 @@ impl<T> [T] { revswap(front_half, back_half, half_len); #[inline] - fn revswap<T>(a: &mut [T], b: &mut [T], n: usize) { - debug_assert_eq!(a.len(), n); - debug_assert_eq!(b.len(), n); + 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 @@ -708,8 +709,10 @@ impl<T> [T] { // lengths of the slices are known -- it's removed. let (a, b) = (&mut a[..n], &mut b[..n]); - for i in 0..n { + let mut i = 0; + while i < n { mem::swap(&mut a[i], &mut b[n - 1 - i]); + i += 1; } } } |
