File tree 1 file changed +41
-0
lines changed
src/test/ui/const-generics/type-dependent
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ // run-pass
2
+ #![ feature( const_generics) ]
3
+ #![ allow( incomplete_features) ]
4
+
5
+ use std:: mem:: MaybeUninit ;
6
+
7
+ trait CollectSlice < ' a > : Iterator {
8
+ fn inner_array < const N : usize > ( & mut self ) -> [ Self :: Item ; N ] ;
9
+
10
+ fn collect_array < const N : usize > ( & mut self ) -> [ Self :: Item ; N ] {
11
+ let result = self . inner_array ( ) ;
12
+ assert ! ( self . next( ) . is_none( ) ) ;
13
+ result
14
+ }
15
+ }
16
+
17
+ impl < ' a , I : ?Sized > CollectSlice < ' a > for I
18
+ where
19
+ I : Iterator ,
20
+ {
21
+ fn inner_array < const N : usize > ( & mut self ) -> [ Self :: Item ; N ] {
22
+ let mut result: [ MaybeUninit < Self :: Item > ; N ] =
23
+ unsafe { MaybeUninit :: uninit ( ) . assume_init ( ) } ;
24
+
25
+ let mut count = 0 ;
26
+ for ( dest, item) in result. iter_mut ( ) . zip ( self ) {
27
+ * dest = MaybeUninit :: new ( item) ;
28
+ count += 1 ;
29
+ }
30
+
31
+ assert_eq ! ( N , count) ;
32
+
33
+ let temp_ptr: * const [ MaybeUninit < Self :: Item > ; N ] = & result;
34
+ unsafe { std:: ptr:: read ( temp_ptr as * const [ Self :: Item ; N ] ) }
35
+ }
36
+ }
37
+
38
+ fn main ( ) {
39
+ let mut foos = [ 0u64 ; 9 ] . iter ( ) . cloned ( ) ;
40
+ let _bar: [ u64 ; 9 ] = foos. collect_array :: < 9_usize > ( ) ;
41
+ }
You can’t perform that action at this time.
0 commit comments