Skip to content

Instantly share code, notes, and snippets.

@AlphaModder
Created January 6, 2022 19:22
Show Gist options
  • Save AlphaModder/7f88dae33d826993fb47aec4f27b5c9e to your computer and use it in GitHub Desktop.
Save AlphaModder/7f88dae33d826993fb47aec4f27b5c9e to your computer and use it in GitHub Desktop.
to_chunks.rs
fn to_chunks<I: Iterator, const N: usize>(mut iter: I) -> impl Iterator<Item=[I::Item; N]> + {
std::iter::from_fn(move || {
let mut buf: [MaybeUninit<I::Item>; N] = unsafe { MaybeUninit::uninit().assume_init() };
for i in 0..N {
match iter.next() {
Some(val) => buf[i].write(val),
None => return None
};
}
Some(unsafe { (&buf as *const _ as *const [I::Item; N]).read() })
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment