#![no_std]
#![doc = include_str!("../README.md")]
pub trait SubsliceToArray<T, const N: usize> {
fn subslice_to_array<const START: usize, const END: usize>(&self) -> [T; N];
}
impl<T: Copy, const N: usize> SubsliceToArray<T, N> for [T] {
#[inline]
fn subslice_to_array<const START: usize, const END: usize>(&self) -> [T; N] {
subslice_to_array::<START, END, T, N>(self)
}
}
pub trait SubsliceToArrayRef<T, const N: usize> {
fn subslice_to_array_ref<const START: usize, const END: usize>(&self) -> &[T; N];
}
impl<T, const N: usize> SubsliceToArrayRef<T, N> for [T] {
#[inline]
fn subslice_to_array_ref<const START: usize, const END: usize>(&self) -> &[T; N] {
subslice_to_array_ref::<START, END, T, N>(self)
}
}
pub trait SubsliceToArrayMut<T, const N: usize> {
fn subslice_to_array_mut<const START: usize, const END: usize>(&mut self) -> &mut [T; N];
}
impl<T, const N: usize> SubsliceToArrayMut<T, N> for [T] {
#[inline]
fn subslice_to_array_mut<const START: usize, const END: usize>(&mut self) -> &mut [T; N] {
subslice_to_array_mut::<START, END, T, N>(self)
}
}
#[inline]
pub fn subslice_to_array<const START: usize, const END: usize, T: Copy, const N: usize>(
slice: &[T],
) -> [T; N] {
*subslice_to_array_ref::<START, END, T, N>(slice)
}
#[inline]
pub fn subslice_to_array_ref<const START: usize, const END: usize, T, const N: usize>(
slice: &[T],
) -> &[T; N] {
const {
assert!(
START.checked_add(N).is_some(),
"`START + N` would overflow a usize in subslice_to_array or subslice_to_array_ref",
);
assert!(
START + N == END,
"subslice_to_array or subslice_to_array_ref was called with incorrect START/END bounds",
);
}
slice[START..END].try_into().unwrap()
}
#[inline]
pub fn subslice_to_array_mut<const START: usize, const END: usize, T, const N: usize>(
slice: &mut [T],
) -> &mut [T; N] {
const {
assert!(
START.checked_add(N).is_some(),
"`START + N` would overflow a usize in subslice_to_array_mut",
);
assert!(
START + N == END,
"subslice_to_array_mut was called with incorrect START/END bounds",
);
}
(&mut slice[START..END]).try_into().unwrap()
}