Skip to content

Commit 97c963d

Browse files
committed
make slice::{split_at,split_at_unchecked} const functions
1 parent 06f4950 commit 97c963d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
#![feature(const_size_of_val)]
138138
#![feature(const_slice_from_raw_parts_mut)]
139139
#![feature(const_slice_ptr_len)]
140+
#![feature(const_slice_split_at_not_mut)]
140141
#![feature(const_str_from_utf8_unchecked_mut)]
141142
#![feature(const_swap)]
142143
#![feature(const_trait_impl)]

library/core/src/slice/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1538,13 +1538,14 @@ impl<T> [T] {
15381538
/// }
15391539
/// ```
15401540
#[stable(feature = "rust1", since = "1.0.0")]
1541+
#[rustc_const_unstable(feature = "const_slice_split_at_not_mut", issue = "none")]
15411542
#[inline]
15421543
#[track_caller]
15431544
#[must_use]
1544-
pub fn split_at(&self, mid: usize) -> (&[T], &[T]) {
1545+
pub const fn split_at(&self, mid: usize) -> (&[T], &[T]) {
15451546
assert!(mid <= self.len());
15461547
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
1547-
// fulfills the requirements of `from_raw_parts_mut`.
1548+
// fulfills the requirements of `split_at_unchecked`.
15481549
unsafe { self.split_at_unchecked(mid) }
15491550
}
15501551

@@ -1623,11 +1624,15 @@ impl<T> [T] {
16231624
/// }
16241625
/// ```
16251626
#[unstable(feature = "slice_split_at_unchecked", reason = "new API", issue = "76014")]
1627+
#[rustc_const_unstable(feature = "const_slice_split_at_not_mut", issue = "none")]
16261628
#[inline]
16271629
#[must_use]
1628-
pub unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) {
1630+
pub const unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) {
1631+
let len = self.len();
1632+
let ptr = self.as_ptr();
1633+
16291634
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
1630-
unsafe { (self.get_unchecked(..mid), self.get_unchecked(mid..)) }
1635+
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) }
16311636
}
16321637

16331638
/// Divides one mutable slice into two at an index, without doing bounds checking.

0 commit comments

Comments
 (0)