rust/library/core/src/slice
Matthias Krüger 8497948c7a
Rollup merge of #95198 - clarfonthey:get_chunk, r=scottmcm
Add slice::{split_,}{first,last}_chunk{,_mut}

This adds to the existing tracking issue for `slice::array_chunks` (#74985) under a separate feature, `slice_get_chunk`.

Currently, we have the existing `first`/`last` API for slices:

```rust
impl [T] {
    pub const fn first(&self) -> Option<&T>;
    pub const fn first_mut(&mut self) -> Option<&mut T>;
    pub const fn last(&self) -> Option<&T>;
    pub const fn last_mut(&mut self) -> Option<&mut T>;
    pub const fn split_first(&self) -> Option<(&T, &[T])>;
    pub const fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])>;
    pub const fn split_last(&self) -> Option<(&T, &[T])>;
    pub const fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])>;
}
```

This augments it with a `first_chunk`/`last_chunk` API that allows retrieving multiple elements at once:

```rust
impl [T] {
    pub const fn first_chunk<const N: usize>(&self) -> Option<&[T; N]>;
    pub const 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 const 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 const 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; N], &[T])>;
    pub const fn split_last_chunk_mut<const N: usize>(&mut self) -> Option<(&mut [T; N], &mut [T])>;
}
```

The code is based off of a copy of the existing API, with the documentation and examples properly modified. Currently, the most common way to perform these kinds of lookups with the existing methods is via `slice.as_chunks::<N>().0[0]` or the worse `slice.as_chunks::<N>().0[slice.len() - N]`, which is substantially less readable than `slice.first_chunk::<N>()` or `slice.last_chunk::<N>()`.

ACP: https://github.com/rust-lang/libs-team/issues/69
2023-05-25 08:01:07 +02:00
..
iter Simplify the implementation of iterators over slices of ZSTs 2023-05-10 13:01:43 -07:00
ascii.rs Tune the is_ascii implementation used for short slices 2023-05-06 22:56:43 -07:00
cmp.rs Merge two different equality specialization traits in core 2023-03-01 14:42:06 -08:00
index.rs rm const traits in libcore 2023-04-16 06:49:27 +00:00
iter.rs Remove useless assumes from slice::iter(_mut) 2023-05-12 17:34:55 -07:00
memchr.rs fix tidy 2023-04-16 07:27:28 +00:00
mod.rs Rollup merge of #95198 - clarfonthey:get_chunk, r=scottmcm 2023-05-25 08:01:07 +02:00
raw.rs Print the precondition we violated, and visible through output capture 2022-10-26 22:09:17 -04:00
rotate.rs Make ZST checks in core/alloc more readable 2022-09-22 23:12:29 -07:00
sort.rs Use code with reliable branchless code-gen for slice::sort merge 2023-05-16 18:38:32 +02:00
specialize.rs Remove size_of == 1 case from fill specialization. 2021-08-09 19:25:28 +02:00