rust/library/core/src
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
..
alloc core is now compilable 2023-04-16 07:20:26 +00:00
array Add an example that depends on is_ascii in a const 2023-05-04 14:46:17 -07:00
ascii Add the basic ascii::Char type 2023-05-03 22:09:33 -07:00
async_iter use consistent terminology 2022-10-29 09:23:12 +02:00
cell replace version placeholders 2023-04-28 08:47:55 -07:00
char ascii::Char-ify the escaping code 2023-05-12 19:37:02 -07:00
cmp Merge two different equality specialization traits in core 2023-03-01 14:42:06 -08:00
convert Add #[inline] to functions that are never called 2023-05-07 12:41:37 +01:00
ffi Rollup merge of #111043 - jmillikin:cstr-is-empty, r=dtolnay 2023-05-17 11:13:56 +05:30
fmt Mark internal functions and traits unsafe 2023-05-15 14:31:00 -04:00
future tidy 2023-05-19 12:45:41 +02:00
hash core is now compilable 2023-04-16 07:20:26 +00:00
intrinsics Documentation 2023-05-15 12:08:16 +02:00
iter Rollup merge of #111612 - ChayimFriedman2:collect-into-slice-ref, r=petrochenkov 2023-05-23 00:32:18 +05:30
macros enable rust_2018_idioms for doctests 2023-05-07 00:12:29 +03:00
mem Rename drop_copy lint to dropping_copy_types 2023-05-21 13:37:32 +02:00
net Inline SocketAddr methods 2023-05-03 11:56:55 +02:00
num Auto merge of #111044 - jmillikin:nonzero-negation, r=dtolnay 2023-05-16 01:07:42 +00:00
ops Auto merge of #103413 - RalfJung:phantom-dropck, r=lcnr 2023-05-13 00:23:51 +00:00
panic Rollup merge of #108356 - gftea:master, r=workingjubilee 2023-05-15 17:12:44 +02:00
prelude Revert "Remove #[alloc_error_handler] from the compiler and library" 2023-04-25 00:08:35 +02:00
ptr drop_in_place docs: remove pseudocode-ish implementation details 2023-05-21 11:34:01 -04:00
slice Rollup merge of #95198 - clarfonthey:get_chunk, r=scottmcm 2023-05-25 08:01:07 +02:00
str Fix some misleading and copy-pasted Pattern examples 2023-05-14 23:03:50 -07:00
sync replace version placeholders 2023-04-28 08:47:55 -07:00
task tidy 2023-05-19 12:45:41 +02:00
unicode Use hex literal for INDEX_MASK 2023-03-21 09:59:47 +01:00
any.rs Hide repr attribute from doc of types without guaranteed repr 2023-05-16 10:00:52 -07:00
arch.rs move core::arch into separate file 2022-11-20 10:28:14 +01:00
ascii.rs ascii::Char-ify the escaping code 2023-05-12 19:37:02 -07:00
asserting.rs [RFC 2011] Library code 2022-05-22 07:18:32 -03:00
bool.rs core is now compilable 2023-04-16 07:20:26 +00:00
borrow.rs rm const traits in libcore 2023-04-16 06:49:27 +00:00
cell.rs add UnsafeCell::from_mut 2023-05-16 15:36:05 -04:00
clone.rs core is now compilable 2023-04-16 07:20:26 +00:00
cmp.rs Add #[inline] to functions that are never called 2023-05-07 12:41:37 +01:00
default.rs rm const traits in libcore 2023-04-16 06:49:27 +00:00
error.md Small round of typo fixes 2022-11-04 20:06:18 -07:00
error.rs Bump to latest beta 2023-03-15 08:55:22 -04:00
escape.rs ascii::Char-ify the escaping code 2023-05-12 19:37:02 -07:00
hint.rs Rollup merge of #108416 - pat-nel87:Issue-107957-black_box_docs, r=jyn514 2023-04-26 01:55:49 -05:00
internal_macros.rs rm const traits in libcore 2023-04-16 06:49:27 +00:00
intrinsics.rs Rollup merge of #111408 - TomMD:patch-1, r=workingjubilee 2023-05-10 06:12:15 +02:00
lib.rs Auto merge of #111524 - scottmcm:escape-using-ascii, r=cuviper 2023-05-20 04:43:17 +00:00
marker.rs Don't use inner macro in marker_impls 2023-05-21 04:16:24 +00:00
option.rs Remove unneeded calls to mem::forget 2023-05-06 14:42:07 -07:00
panic.rs Shorten lifetime of even more panic temporaries 2023-05-15 03:47:37 -07:00
panicking.rs handle cfg(bootstrap) 2023-04-28 08:47:55 -07:00
pin.rs Document Pin memory layout 2023-05-18 01:30:12 -04:00
primitive.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
primitive_docs.rs Auto merge of #106621 - ozkanonur:enable-elided-lifetimes-for-doctests, r=Mark-Simulacrum 2023-05-08 04:50:28 +00:00
result.rs replace version placeholders 2023-04-28 08:47:55 -07:00
time.rs Use fmt::Alignment instead of fmt::rt::v1::Alignment. 2023-04-20 18:03:47 +02:00
tuple.rs clean up transmutes in core 2023-05-06 13:28:38 +02:00
unit.rs Use implicit capture syntax in format_args 2022-03-10 10:23:40 -05:00