array_bytes/
op.rs

1mod slice;
2pub use slice::*;
3
4mod vec;
5pub use vec::*;
6
7// core
8use core::cmp::Ordering;
9
10/// Prefixes the given element to the given array/slice/vector to make it a fixed-size array of
11/// length `N`.
12///
13/// If the length of the array/slice/vector is already equal to `N`, it returns the
14/// array/slice/vector as a fixed-size array.
15/// If the length of the array/slice/vector is greater than `N`, it returns the first `N` elements
16/// of the array/slice/vector as a fixed-size array.
17/// If the length of the array/slice/vector is less than `N`, it creates a new fixed-size array of
18/// length `N` and copies the array/slice/vector into it, padding the remaining elements with the
19/// given element.
20///
21/// # Examples
22/// ```
23/// assert_eq!(array_bytes::prefix_with::<_, _, 4>([5, 2, 0, 1], 0), [5, 2, 0, 1]);
24/// assert_eq!(array_bytes::prefix_with::<_, _, 4>([5, 2, 0, 1, 3, 1, 4], 0), [5, 2, 0, 1]);
25/// assert_eq!(array_bytes::prefix_with::<_, _, 5>([5, 2, 0], 0), [0, 0, 5, 2, 0]);
26/// ```
27pub fn prefix_with<A, T, const N: usize>(any: A, element: T) -> [T; N]
28where
29	A: AsRef<[T]>,
30	T: Copy,
31{
32	pad_array(any, element, true)
33}
34#[test]
35fn prefix_with_should_work() {
36	assert_eq!(prefix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
37	assert_eq!(prefix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
38	assert_eq!(prefix_with::<_, _, 5>([1, 2, 3], 0), [0, 0, 1, 2, 3]);
39}
40
41/// Suffixes the given element to the given array/slice/vector to make it a fixed-size array of
42/// length `N`.
43///
44/// If the length of the array/slice/vector is already equal to `N`, it returns the
45/// array/slice/vector as a fixed-size array.
46/// If the length of the array/slice/vector is greater than `N`, it returns the first `N` elements
47/// of the array/slice/vector as a fixed-size array.
48/// If the length of the array/slice/vector is less than `N`, it creates a new fixed-size array of
49/// length `N` and copies the array/slice/vector into it, padding the remaining elements with the
50/// given element.
51///
52/// # Examples
53/// ```
54/// assert_eq!(array_bytes::suffix_with::<_, _, 4>([5, 2, 0, 1], 0), [5, 2, 0, 1]);
55/// assert_eq!(array_bytes::suffix_with::<_, _, 4>([5, 2, 0, 1, 3, 1, 4], 0), [5, 2, 0, 1]);
56/// assert_eq!(array_bytes::suffix_with::<_, _, 5>([5, 2, 0], 0), [5, 2, 0, 0, 0]);
57/// ```
58pub fn suffix_with<A, T, const N: usize>(any: A, element: T) -> [T; N]
59where
60	A: AsRef<[T]>,
61	T: Copy,
62{
63	pad_array(any, element, false)
64}
65#[test]
66fn suffix_with_should_work() {
67	assert_eq!(suffix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
68	assert_eq!(suffix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
69	assert_eq!(suffix_with::<_, _, 5>([1, 2, 3], 0), [1, 2, 3, 0, 0]);
70}
71
72#[inline(always)]
73fn pad_array<A, T, const N: usize>(any: A, element: T, pad_start: bool) -> [T; N]
74where
75	A: AsRef<[T]>,
76	T: Copy,
77{
78	let a = any.as_ref();
79
80	match a.len().cmp(&N) {
81		Ordering::Equal => slice2array(a).expect("`a.len() == N`; qed"),
82		Ordering::Greater => slice2array(&a[..N]).expect("`a[..N]` has exactly `N` elements; qed"),
83		Ordering::Less => {
84			let mut padded = [element; N];
85
86			if pad_start {
87				padded[N - a.len()..].copy_from_slice(a);
88			} else {
89				padded[..a.len()].copy_from_slice(a);
90			}
91
92			padded
93		},
94	}
95}