Files
big-c/src/helper.rs
Aryadev Chavali ec31844ee7 helper: new ExactSizedArr adaptor for ExactSizedIterators
Given an ExactSizedIterator and a compile time known size (N), we
should be able to generate stack allocated arrays of the contents of
an iterator as long as the iterator has at least the required size.
2026-04-07 12:36:48 +01:00

31 lines
862 B
Rust

/** Given an array of arguments, return them sorted.
Best utilised with array destructuring. */
pub fn ordered<T: Ord, const N: usize>(mut xs: [T; N]) -> [T; N] {
xs.sort();
xs
}
/** An iterator adaptor (derived from ExactSizedIterator) which has a guaranteed
compile time size, allowing for collection of an iterator into a stack allocated
array. */
pub trait ExactSizedArr<I>: ExactSizeIterator<Item = I> + Sized
where
I: Default,
{
fn into_array<const N: usize>(mut self) -> Result<[Self::Item; N], Self> {
if self.len() < N {
Err(self)
} else {
Ok(std::array::from_fn(|_| self.next().unwrap()))
}
}
}
/** Default implementation of ExactSizedArr for any ExactSizeIterator. */
impl<T, I> ExactSizedArr<T> for I
where
T: Default + Copy + Clone,
I: ExactSizeIterator<Item = T>,
{
}