Rollup merge of #100076 - tspiteri:const_slice_split_at, r=oli-obk
make slice::{split_at,split_at_unchecked} const functions Now that `slice::from_raw_parts` is const in stable 1.64, it makes sense to have `split_at` const as well, otherwise unsafe code is required to achieve a const equivalent.
This commit is contained in:
commit
e193f4697f
2 changed files with 14 additions and 4 deletions
|
@ -149,6 +149,7 @@
|
||||||
#![feature(maybe_uninit_uninit_array)]
|
#![feature(maybe_uninit_uninit_array)]
|
||||||
#![feature(ptr_metadata)]
|
#![feature(ptr_metadata)]
|
||||||
#![feature(slice_ptr_get)]
|
#![feature(slice_ptr_get)]
|
||||||
|
#![feature(slice_split_at_unchecked)]
|
||||||
#![feature(str_internals)]
|
#![feature(str_internals)]
|
||||||
#![feature(utf16_extra)]
|
#![feature(utf16_extra)]
|
||||||
#![feature(utf16_extra_const)]
|
#![feature(utf16_extra_const)]
|
||||||
|
|
|
@ -1541,13 +1541,14 @@ impl<T> [T] {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[rustc_const_unstable(feature = "const_slice_split_at_not_mut", issue = "none")]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn split_at(&self, mid: usize) -> (&[T], &[T]) {
|
pub const fn split_at(&self, mid: usize) -> (&[T], &[T]) {
|
||||||
assert!(mid <= self.len());
|
assert!(mid <= self.len());
|
||||||
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
|
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
|
||||||
// fulfills the requirements of `from_raw_parts_mut`.
|
// fulfills the requirements of `split_at_unchecked`.
|
||||||
unsafe { self.split_at_unchecked(mid) }
|
unsafe { self.split_at_unchecked(mid) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1626,11 +1627,19 @@ impl<T> [T] {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "slice_split_at_unchecked", reason = "new API", issue = "76014")]
|
#[unstable(feature = "slice_split_at_unchecked", reason = "new API", issue = "76014")]
|
||||||
|
#[rustc_const_unstable(feature = "slice_split_at_unchecked", issue = "76014")]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) {
|
pub const unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) {
|
||||||
|
// HACK: the const function `from_raw_parts` is used to make this
|
||||||
|
// function const; previously the implementation used
|
||||||
|
// `(self.get_unchecked(..mid), self.get_unchecked(mid..))`
|
||||||
|
|
||||||
|
let len = self.len();
|
||||||
|
let ptr = self.as_ptr();
|
||||||
|
|
||||||
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
|
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
|
||||||
unsafe { (self.get_unchecked(..mid), self.get_unchecked(mid..)) }
|
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Divides one mutable slice into two at an index, without doing bounds checking.
|
/// Divides one mutable slice into two at an index, without doing bounds checking.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue