Skip to content

Commit 64461da

Browse files
authored
Rollup merge of #117561 - tgross35:split-array, r=scottmcm
Stabilize `slice_first_last_chunk` This PR does a few different things based around stabilizing `slice_first_last_chunk`. They are split up so this PR can be by-commit reviewed, I can move parts to a separate PR if desired. This feature provides a very elegant API to extract arrays from either end of a slice, such as for parsing integers from binary data. ## Stabilize `slice_first_last_chunk` ACP: rust-lang/libs-team#69 Implementation: #90091 Tracking issue: #111774 This stabilizes the functionality from #111774: ```rust impl [T] { pub const fn first_chunk<const N: usize>(&self) -> Option<&[T; N]>; pub fn first_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]>; pub const fn last_chunk<const N: usize>(&self) -> Option<&[T; N]>; pub fn last_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]>; pub const fn split_first_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])>; pub fn split_first_chunk_mut<const N: usize>(&mut self) -> Option<(&mut [T; N], &mut [T])>; pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T], &[T; N])>; pub fn split_last_chunk_mut<const N: usize>(&mut self) -> Option<(&mut [T], &mut [T; N])>; } ``` Const stabilization is included for all non-mut methods, which are blocked on `const_mut_refs`. This change includes marking the trivial function `slice_split_at_unchecked` const-stable for internal use (but not fully stable). ## Remove `split_array` slice methods Tracking issue: #90091 Implementation: #83233 (review) This PR also removes the following unstable methods from the `split_array` feature, #90091: ```rust impl<T> [T] { pub fn split_array_ref<const N: usize>(&self) -> (&[T; N], &[T]); pub fn split_array_mut<const N: usize>(&mut self) -> (&mut [T; N], &mut [T]); pub fn rsplit_array_ref<const N: usize>(&self) -> (&[T], &[T; N]); pub fn rsplit_array_mut<const N: usize>(&mut self) -> (&mut [T], &mut [T; N]); } ``` This is done because discussion at #90091 and its implementation PR indicate a strong preference for nonpanicking APIs that return `Option`. The only difference between functions under the `split_array` and `slice_first_last_chunk` features is `Option` vs. panic, so remove the duplicates as part of this stabilization. This does not affect the array methods from `split_array`. We will want to revisit these once `generic_const_exprs` is further along. ## Reverse order of return tuple for `split_last_chunk{,_mut}` An unresolved question for #111774 is whether to return `(preceding_slice, last_chunk)` (`(&[T], &[T; N])`) or the reverse (`(&[T; N], &[T])`), from `split_last_chunk` and `split_last_chunk_mut`. It is currently implemented as `(last_chunk, preceding_slice)` which matches `split_last -> (&T, &[T])`. The first commit changes these to `(&[T], &[T; N])` for these reasons: - More consistent with other splitting methods that return multiple values: `str::rsplit_once`, `slice::split_at{,_mut}`, `slice::align_to` all return tuples with the items in order - More intuitive (arguably opinion, but it is consistent with other language elements like pattern matching `let [a, b, rest @ ..] ...` - If we ever added a varidic way to obtain multiple chunks, it would likely return something in order: `.split_many_last::<(2, 4)>() -> (&[T], &[T; 2], &[T; 4])` - It is the ordering used in the `rsplit_array` methods I think the inconsistency with `split_last` could be acceptable in this case, since for `split_last` the scalar `&T` doesn't have any internal order to maintain with the other items. ## Unresolved questions Do we want to reserve the same names on `[u8; N]` to avoid inference confusion? #117561 (comment) --- `slice_first_last_chunk` has only been around since early 2023, but `split_array` has been around since 2021. `@rustbot` label -T-libs +T-libs-api -T-libs +needs-fcp cc `@rust-lang/wg-const-eval,` `@scottmcm` who raised this topic, `@clarfonthey` implementer of `slice_first_last_chunk` `@jethrogb` implementer of `split_array` Zulip discussion: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Stabilizing.20array-from-slice.20*something*.3F Fixes: #111774
2 parents 32ec40c + 500d6f6 commit 64461da

File tree

4 files changed

+90
-261
lines changed

4 files changed

+90
-261
lines changed

compiler/rustc_serialize/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#![feature(min_specialization)]
1717
#![feature(never_type)]
1818
#![feature(ptr_sub_ptr)]
19-
#![feature(slice_first_last_chunk)]
2019
#![cfg_attr(test, feature(test))]
2120
#![allow(rustc::internal)]
2221
#![deny(rustc::untranslatable_diagnostic)]

library/core/src/array/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl<T, const N: usize> [T; N] {
647647
)]
648648
#[inline]
649649
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T]) {
650-
(&self[..]).split_array_ref::<M>()
650+
(&self[..]).split_first_chunk::<M>().unwrap()
651651
}
652652

653653
/// Divides one mutable array reference into two at an index.
@@ -680,7 +680,7 @@ impl<T, const N: usize> [T; N] {
680680
)]
681681
#[inline]
682682
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T]) {
683-
(&mut self[..]).split_array_mut::<M>()
683+
(&mut self[..]).split_first_chunk_mut::<M>().unwrap()
684684
}
685685

686686
/// Divides one array reference into two at an index from the end.
@@ -725,7 +725,7 @@ impl<T, const N: usize> [T; N] {
725725
)]
726726
#[inline]
727727
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M]) {
728-
(&self[..]).rsplit_array_ref::<M>()
728+
(&self[..]).split_last_chunk::<M>().unwrap()
729729
}
730730

731731
/// Divides one mutable array reference into two at an index from the end.
@@ -758,7 +758,7 @@ impl<T, const N: usize> [T; N] {
758758
)]
759759
#[inline]
760760
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M]) {
761-
(&mut self[..]).rsplit_array_mut::<M>()
761+
(&mut self[..]).split_last_chunk_mut::<M>().unwrap()
762762
}
763763
}
764764

0 commit comments

Comments
 (0)