1
0
Fork 0
forked from wry/wry

utils: move damage algorithms to algorithm crate

This commit is contained in:
Julian Orth 2022-06-03 21:01:20 +02:00
parent 259340938b
commit 6e244a08ab
12 changed files with 710 additions and 542 deletions

35
algorithms/src/windows.rs Normal file
View file

@ -0,0 +1,35 @@
pub trait WindowsExt<T> {
type Windows<'a, const N: usize>: Iterator<Item = &'a [T; N]>
where
Self: 'a,
T: 'a;
fn array_windows_ext<'a, const N: usize>(&'a self) -> Self::Windows<'a, N>;
}
impl<T> WindowsExt<T> for [T] {
type Windows<'a, const N: usize> = WindowsIter<'a, T, N> where T: 'a;
fn array_windows_ext<'a, const N: usize>(&'a self) -> Self::Windows<'a, N> {
WindowsIter { slice: self }
}
}
pub struct WindowsIter<'a, T, const N: usize> {
slice: &'a [T],
}
impl<'a, T, const N: usize> Iterator for WindowsIter<'a, T, N> {
type Item = &'a [T; N];
fn next(&mut self) -> Option<Self::Item> {
if self.slice.len() < N {
return None;
}
let res = unsafe { &*self.slice.as_ptr().cast::<[T; N]>() };
if N > 0 {
self.slice = &self.slice[1..];
}
Some(res)
}
}