diff options
Diffstat (limited to 'library/core/src/array')
| -rw-r--r-- | library/core/src/array/mod.rs | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 089f1f36390..09bb4519170 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -339,9 +339,7 @@ impl<T: Copy, const N: usize> Copy for [T; N] {} impl<T: Clone, const N: usize> Clone for [T; N] { #[inline] fn clone(&self) -> Self { - // SAFETY: we know for certain that this iterator will yield exactly `N` - // items. - unsafe { collect_into_array_unchecked(&mut self.iter().cloned()) } + SpecArrayClone::clone(self) } #[inline] @@ -350,6 +348,29 @@ impl<T: Clone, const N: usize> Clone for [T; N] { } } +#[cfg(not(bootstrap))] +trait SpecArrayClone: Clone { + fn clone<const N: usize>(array: &[Self; N]) -> [Self; N]; +} + +#[cfg(not(bootstrap))] +impl<T: Clone> SpecArrayClone for T { + #[inline] + default fn clone<const N: usize>(array: &[T; N]) -> [T; N] { + // SAFETY: we know for certain that this iterator will yield exactly `N` + // items. + unsafe { collect_into_array_unchecked(&mut array.iter().cloned()) } + } +} + +#[cfg(not(bootstrap))] +impl<T: Copy> SpecArrayClone for T { + #[inline] + fn clone<const N: usize>(array: &[T; N]) -> [T; N] { + *array + } +} + // The Default impls cannot be done with const generics because `[T; 0]` doesn't // require Default to be implemented, and having different impl blocks for // different numbers isn't supported yet. |
