mod slice;
pub use slice::*;
mod vec;
pub use vec::*;
use core::cmp::Ordering;
pub fn prefix_with<A, T, const N: usize>(any: A, element: T) -> [T; N]
where
A: AsRef<[T]>,
T: Copy,
{
pad_array(any, element, true)
}
#[test]
fn prefix_with_should_work() {
assert_eq!(prefix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
assert_eq!(prefix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
assert_eq!(prefix_with::<_, _, 5>([1, 2, 3], 0), [0, 0, 1, 2, 3]);
}
pub fn suffix_with<A, T, const N: usize>(any: A, element: T) -> [T; N]
where
A: AsRef<[T]>,
T: Copy,
{
pad_array(any, element, false)
}
#[test]
fn suffix_with_should_work() {
assert_eq!(suffix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
assert_eq!(suffix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
assert_eq!(suffix_with::<_, _, 5>([1, 2, 3], 0), [1, 2, 3, 0, 0]);
}
#[inline(always)]
fn pad_array<A, T, const N: usize>(any: A, element: T, pad_start: bool) -> [T; N]
where
A: AsRef<[T]>,
T: Copy,
{
let a = any.as_ref();
match a.len().cmp(&N) {
Ordering::Equal => slice2array(a).expect("`a.len() == N`; qed"),
Ordering::Greater => slice2array(&a[..N]).expect("`a[..N]` has exactly `N` elements; qed"),
Ordering::Less => {
let mut padded = [element; N];
if pad_start {
padded[N - a.len()..].copy_from_slice(a);
} else {
padded[..a.len()].copy_from_slice(a);
}
padded
},
}
}