1 | // Permission is hereby granted, free of charge, to any |
2 | // person obtaining a copy of this software and associated |
3 | // documentation files (the "Software"), to deal in the |
4 | // Software without restriction, including without |
5 | // limitation the rights to use, copy, modify, merge, |
6 | // publish, distribute, sublicense, and/or sell copies of |
7 | // the Software, and to permit persons to whom the Software |
8 | // is furnished to do so, subject to the following |
9 | // conditions: |
10 | // |
11 | // The above copyright notice and this permission notice |
12 | // shall be included in all copies or substantial portions |
13 | // of the Software. |
14 | // |
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
16 | // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
18 | // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
19 | // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
20 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
21 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
22 | // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
23 | // DEALINGS IN THE SOFTWARE. |
24 | |
25 | mod as_chunks; |
26 | mod as_chunks_mut; |
27 | |
28 | pub use as_chunks::{as_chunks, AsChunks}; |
29 | pub use as_chunks_mut::{as_chunks_mut, AsChunksMut}; |
30 | |
31 | // TODO(MSRV feature(split_at_checked)): Use `slice::split_at_checked`. |
32 | // |
33 | // Note that the libcore version is implemented in terms of |
34 | // `slice::split_at_unchecked()`, and `slice::split_at()` was changed to be |
35 | // implemented in terms of `split_at_checked`. For now, we implement this in |
36 | // terms of `split_at` and rely on the optimizer to eliminate the panic. |
37 | #[inline (always)] |
38 | pub fn split_at_checked<T>(slice: &[T], i: usize) -> Option<(&[T], &[T])> { |
39 | if slice.len() >= i { |
40 | Some(slice.split_at(mid:i)) |
41 | } else { |
42 | None |
43 | } |
44 | } |
45 | |
46 | // TODO(MSRV-1.77): Use `slice::split_first_chunk_mut`. |
47 | #[inline (always)] |
48 | pub fn split_first_chunk_mut<T, const N: usize>( |
49 | slice: &mut [T], |
50 | ) -> Option<(&mut [T; N], &mut [T])> { |
51 | if slice.len() >= N { |
52 | let (head: &mut [T], tail: &mut [T]) = slice.split_at_mut(N); |
53 | head.try_into().ok().map(|head: &mut [T; N]| (head, tail)) |
54 | } else { |
55 | None |
56 | } |
57 | } |
58 | |