Created
January 6, 2022 19:22
-
-
Save AlphaModder/7f88dae33d826993fb47aec4f27b5c9e to your computer and use it in GitHub Desktop.
to_chunks.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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