Skip to content

Commit

Permalink
add regression test for rust-lang#71805
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Jul 16, 2020
1 parent a2b1827 commit de8d2e8
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/test/ui/const-generics/type-dependent/issue-71805.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// run-pass
#![feature(const_generics)]
#![allow(incomplete_features)]

use std::mem::MaybeUninit;

trait CollectSlice<'a>: Iterator {
fn inner_array<const N: usize>(&mut self) -> [Self::Item; N];

fn collect_array<const N: usize>(&mut self) -> [Self::Item; N] {
let result = self.inner_array();
assert!(self.next().is_none());
result
}
}

impl<'a, I: ?Sized> CollectSlice<'a> for I
where
I: Iterator,
{
fn inner_array<const N: usize>(&mut self) -> [Self::Item; N] {
let mut result: [MaybeUninit<Self::Item>; N] =
unsafe { MaybeUninit::uninit().assume_init() };

let mut count = 0;
for (dest, item) in result.iter_mut().zip(self) {
*dest = MaybeUninit::new(item);
count += 1;
}

assert_eq!(N, count);

let temp_ptr: *const [MaybeUninit<Self::Item>; N] = &result;
unsafe { std::ptr::read(temp_ptr as *const [Self::Item; N]) }
}
}

fn main() {
let mut foos = [0u64; 9].iter().cloned();
let _bar: [u64; 9] = foos.collect_array::<9_usize>();
}

0 comments on commit de8d2e8

Please sign in to comment.