Skip to content

Commit 949a4da

Browse files
committed
Stabilize const_slice_split_at_mut and const_slice_first_last_chunk
1 parent 42ff2ee commit 949a4da

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

library/core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@
150150
#![feature(const_size_of_val_raw)]
151151
#![feature(const_slice_from_raw_parts_mut)]
152152
#![feature(const_slice_from_ref)]
153-
#![feature(const_slice_split_at_mut)]
154153
#![feature(const_str_as_mut)]
155154
#![feature(const_str_from_utf8_unchecked_mut)]
156155
#![feature(const_strict_overflow_ops)]

library/core/src/slice/mod.rs

+73-18
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ impl<T> [T] {
153153
#[inline]
154154
#[must_use]
155155
pub const fn first(&self) -> Option<&T> {
156-
if let [first, ..] = self { Some(first) } else { None }
156+
if let [first, ..] = self {
157+
Some(first)
158+
} else {
159+
None
160+
}
157161
}
158162

159163
/// Returns a mutable reference to the first element of the slice, or `None` if it is empty.
@@ -176,7 +180,11 @@ impl<T> [T] {
176180
#[inline]
177181
#[must_use]
178182
pub const fn first_mut(&mut self) -> Option<&mut T> {
179-
if let [first, ..] = self { Some(first) } else { None }
183+
if let [first, ..] = self {
184+
Some(first)
185+
} else {
186+
None
187+
}
180188
}
181189

182190
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -196,7 +204,11 @@ impl<T> [T] {
196204
#[inline]
197205
#[must_use]
198206
pub const fn split_first(&self) -> Option<(&T, &[T])> {
199-
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
207+
if let [first, tail @ ..] = self {
208+
Some((first, tail))
209+
} else {
210+
None
211+
}
200212
}
201213

202214
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -218,7 +230,11 @@ impl<T> [T] {
218230
#[inline]
219231
#[must_use]
220232
pub const fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
221-
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
233+
if let [first, tail @ ..] = self {
234+
Some((first, tail))
235+
} else {
236+
None
237+
}
222238
}
223239

224240
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
@@ -238,7 +254,11 @@ impl<T> [T] {
238254
#[inline]
239255
#[must_use]
240256
pub const fn split_last(&self) -> Option<(&T, &[T])> {
241-
if let [init @ .., last] = self { Some((last, init)) } else { None }
257+
if let [init @ .., last] = self {
258+
Some((last, init))
259+
} else {
260+
None
261+
}
242262
}
243263

244264
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
@@ -260,7 +280,11 @@ impl<T> [T] {
260280
#[inline]
261281
#[must_use]
262282
pub const fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
263-
if let [init @ .., last] = self { Some((last, init)) } else { None }
283+
if let [init @ .., last] = self {
284+
Some((last, init))
285+
} else {
286+
None
287+
}
264288
}
265289

266290
/// Returns the last element of the slice, or `None` if it is empty.
@@ -279,7 +303,11 @@ impl<T> [T] {
279303
#[inline]
280304
#[must_use]
281305
pub const fn last(&self) -> Option<&T> {
282-
if let [.., last] = self { Some(last) } else { None }
306+
if let [.., last] = self {
307+
Some(last)
308+
} else {
309+
None
310+
}
283311
}
284312

285313
/// Returns a mutable reference to the last item in the slice, or `None` if it is empty.
@@ -302,7 +330,11 @@ impl<T> [T] {
302330
#[inline]
303331
#[must_use]
304332
pub const fn last_mut(&mut self) -> Option<&mut T> {
305-
if let [.., last] = self { Some(last) } else { None }
333+
if let [.., last] = self {
334+
Some(last)
335+
} else {
336+
None
337+
}
306338
}
307339

308340
/// Returns an array reference to the first `N` items in the slice.
@@ -353,7 +385,8 @@ impl<T> [T] {
353385
/// ```
354386
#[inline]
355387
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
356-
#[rustc_const_unstable(feature = "const_slice_first_last_chunk", issue = "111774")]
388+
#[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "CURRENT_RUSTC_VERSION")]
389+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
357390
pub const fn first_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]> {
358391
if self.len() < N {
359392
None
@@ -384,6 +417,7 @@ impl<T> [T] {
384417
#[inline]
385418
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
386419
#[rustc_const_stable(feature = "slice_first_last_chunk", since = "1.77.0")]
420+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
387421
pub const fn split_first_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])> {
388422
if self.len() < N {
389423
None
@@ -418,7 +452,8 @@ impl<T> [T] {
418452
/// ```
419453
#[inline]
420454
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
421-
#[rustc_const_unstable(feature = "const_slice_first_last_chunk", issue = "111774")]
455+
#[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "CURRENT_RUSTC_VERSION")]
456+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
422457
pub const fn split_first_chunk_mut<const N: usize>(
423458
&mut self,
424459
) -> Option<(&mut [T; N], &mut [T])> {
@@ -454,6 +489,7 @@ impl<T> [T] {
454489
#[inline]
455490
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
456491
#[rustc_const_stable(feature = "slice_first_last_chunk", since = "1.77.0")]
492+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
457493
pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T], &[T; N])> {
458494
if self.len() < N {
459495
None
@@ -488,7 +524,8 @@ impl<T> [T] {
488524
/// ```
489525
#[inline]
490526
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
491-
#[rustc_const_unstable(feature = "const_slice_first_last_chunk", issue = "111774")]
527+
#[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "CURRENT_RUSTC_VERSION")]
528+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
492529
pub const fn split_last_chunk_mut<const N: usize>(
493530
&mut self,
494531
) -> Option<(&mut [T], &mut [T; N])> {
@@ -524,6 +561,7 @@ impl<T> [T] {
524561
#[inline]
525562
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
526563
#[rustc_const_stable(feature = "const_slice_last_chunk", since = "1.80.0")]
564+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
527565
pub const fn last_chunk<const N: usize>(&self) -> Option<&[T; N]> {
528566
if self.len() < N {
529567
None
@@ -557,7 +595,8 @@ impl<T> [T] {
557595
/// ```
558596
#[inline]
559597
#[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
560-
#[rustc_const_unstable(feature = "const_slice_first_last_chunk", issue = "111774")]
598+
#[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "CURRENT_RUSTC_VERSION")]
599+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
561600
pub const fn last_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]> {
562601
if self.len() < N {
563602
None
@@ -1900,7 +1939,8 @@ impl<T> [T] {
19001939
#[inline]
19011940
#[track_caller]
19021941
#[must_use]
1903-
#[rustc_const_unstable(feature = "const_slice_split_at_mut", issue = "101804")]
1942+
#[rustc_const_stable(feature = "const_slice_split_at_mut", since = "CURRENT_RUSTC_VERSION")]
1943+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
19041944
pub const fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
19051945
match self.split_at_mut_checked(mid) {
19061946
Some(pair) => pair,
@@ -2002,7 +2042,9 @@ impl<T> [T] {
20022042
/// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
20032043
/// ```
20042044
#[stable(feature = "slice_split_at_unchecked", since = "1.79.0")]
2005-
#[rustc_const_unstable(feature = "const_slice_split_at_mut", issue = "101804")]
2045+
#[rustc_const_stable(feature = "const_slice_split_at_mut", since = "CURRENT_RUSTC_VERSION")]
2046+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
2047+
#[rustc_allow_const_fn_unstable(const_slice_from_raw_parts_mut)]
20062048
#[inline]
20072049
#[must_use]
20082050
pub const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
@@ -2102,7 +2144,8 @@ impl<T> [T] {
21022144
/// assert_eq!(None, v.split_at_mut_checked(7));
21032145
/// ```
21042146
#[stable(feature = "split_at_checked", since = "1.80.0")]
2105-
#[rustc_const_unstable(feature = "const_slice_split_at_mut", issue = "101804")]
2147+
#[rustc_const_stable(feature = "const_slice_split_at_mut", since = "CURRENT_RUSTC_VERSION")]
2148+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
21062149
#[inline]
21072150
#[must_use]
21082151
pub const fn split_at_mut_checked(&mut self, mid: usize) -> Option<(&mut [T], &mut [T])> {
@@ -3814,7 +3857,11 @@ impl<T> [T] {
38143857
//
38153858
// Luckily since all this is constant-evaluated... performance here matters not!
38163859
const fn gcd(a: usize, b: usize) -> usize {
3817-
if b == 0 { a } else { gcd(b, a % b) }
3860+
if b == 0 {
3861+
a
3862+
} else {
3863+
gcd(b, a % b)
3864+
}
38183865
}
38193866

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

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

4590-
if offset < self.len() { Some(offset) } else { None }
4637+
if offset < self.len() {
4638+
Some(offset)
4639+
} else {
4640+
None
4641+
}
45914642
}
45924643

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

4644-
if start <= self.len() && end <= self.len() { Some(start..end) } else { None }
4695+
if start <= self.len() && end <= self.len() {
4696+
Some(start..end)
4697+
} else {
4698+
None
4699+
}
46454700
}
46464701
}
46474702

0 commit comments

Comments
 (0)