1pub const fn pad<const N: usize, const M: usize>(input: [u8; M]) -> [u8; N] {
7 assert!(N >= M);
8
9 let mut out = [0u8; N];
10 let mut idx = 0;
11 loop {
12 if idx >= M {
13 break;
14 }
15 out[idx] = input[idx];
16 idx += 1;
17 }
18 out
19}
20
21pub trait ArrayExt<const N: usize> {
24 fn split_array_ref_stable<const M: usize>(&self) -> (&[u8; M], &[u8]);
30
31 fn rsplit_array_ref_stable<const M: usize>(&self) -> (&[u8], &[u8; M]);
37}
38
39impl<const N: usize> ArrayExt<N> for [u8; N] {
40 #[inline]
41 fn split_array_ref_stable<const M: usize>(&self) -> (&[u8; M], &[u8]) {
42 self[..].split_first_chunk::<M>().unwrap()
43 }
44
45 #[inline]
46 fn rsplit_array_ref_stable<const M: usize>(&self) -> (&[u8], &[u8; M]) {
47 self[..].split_last_chunk::<M>().unwrap()
48 }
49}
50
51#[cfg(test)]
52mod test {
53 use crate::array;
54
55 #[test]
56 fn test_pad() {
57 let input = *b"hello";
58 let actual = array::pad(input);
59 let expected = *b"hello\x00\x00\x00\x00\x00";
60 assert_eq!(actual, expected);
61
62 let actual = array::pad(input);
63 let expected = *b"hello";
64 assert_eq!(actual, expected);
65 }
66}