Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2021 23:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
use std::{
fn main() {
let bar = vec![1,2,3];
let qux: Foo<i32, 3> = bar.into_iter().collect();
// coherence-mollifying newtype,
// stdlib could implement directly on Option<[T; N]>
// (unless something unexpected overlaps?)
struct Foo<T, const N: usize>(Option<[T; N]>);
impl<T, const N: usize> FromIterator<T> for Foo<T, N> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self {
let mut iter = iter.into_iter();
let mut array: [MaybeUninit<T>; N] = MaybeUninit::uninit_array();
for i in 0..N {
if let Some(e) = {
array[i] = MaybeUninit::new(e);
} else {
return Foo(None);
let array = unsafe {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment