autocommit 2022-04-04 23:09:39 CEST
This commit is contained in:
parent
e897d271af
commit
5f79aab15f
21 changed files with 870 additions and 731 deletions
40
src/utils/windows.rs
Normal file
40
src/utils/windows.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use crate::utils::ptr_ext::PtrExt;
|
||||
|
||||
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>
|
||||
where
|
||||
T: 'a,
|
||||
= WindowsIter<'a, T, N>;
|
||||
|
||||
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]>().deref() };
|
||||
if N > 0 {
|
||||
self.slice = &self.slice[1..];
|
||||
}
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue