File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -339,9 +339,7 @@ impl<T: Copy, const N: usize> Copy for [T; N] {}
339339impl < T : Clone , const N : usize > Clone for [ T ; N ] {
340340 #[ inline]
341341 fn clone ( & self ) -> Self {
342- // SAFETY: we know for certain that this iterator will yield exactly `N`
343- // items.
344- unsafe { collect_into_array_unchecked ( & mut self . iter ( ) . cloned ( ) ) }
342+ SpecArrayClone :: clone ( self )
345343 }
346344
347345 #[ inline]
@@ -350,6 +348,29 @@ impl<T: Clone, const N: usize> Clone for [T; N] {
350348 }
351349}
352350
351+ #[ cfg( not( bootstrap) ) ]
352+ trait SpecArrayClone : Clone {
353+ fn clone < const N : usize > ( array : & [ Self ; N ] ) -> [ Self ; N ] ;
354+ }
355+
356+ #[ cfg( not( bootstrap) ) ]
357+ impl < T : Clone > SpecArrayClone for T {
358+ #[ inline]
359+ default fn clone < const N : usize > ( array : & [ T ; N ] ) -> [ T ; N ] {
360+ // SAFETY: we know for certain that this iterator will yield exactly `N`
361+ // items.
362+ unsafe { collect_into_array_unchecked ( & mut array. iter ( ) . cloned ( ) ) }
363+ }
364+ }
365+
366+ #[ cfg( not( bootstrap) ) ]
367+ impl < T : Copy > SpecArrayClone for T {
368+ #[ inline]
369+ fn clone < const N : usize > ( array : & [ T ; N ] ) -> [ T ; N ] {
370+ * array
371+ }
372+ }
373+
353374// The Default impls cannot be done with const generics because `[T; 0]` doesn't
354375// require Default to be implemented, and having different impl blocks for
355376// different numbers isn't supported yet.
Original file line number Diff line number Diff line change 1+ // compile-flags: -O
2+
3+ #![ crate_type = "lib" ]
4+
5+ // CHECK-LABEL: @array_clone
6+ #[ no_mangle]
7+ pub fn array_clone ( a : & [ u8 ; 2 ] ) -> [ u8 ; 2 ] {
8+ // CHECK-NOT: getelementptr
9+ // CHECK-NOT: load i8
10+ // CHECK-NOT: zext
11+ // CHECK-NOT: shl
12+ // CHECK: load i16
13+ // CHECK-NEXT: ret i16
14+ a. clone ( )
15+ }
You can’t perform that action at this time.
0 commit comments