Skip to content

Commit 6f6d832

Browse files
committed
Auto merge of rust-lang#86041 - bstrie:unmagic-array-copy, r=jackh726
Replace Copy/Clone compiler magic on arrays with library impls With const generics the compiler no longer needs to fake these impls.
2 parents 534c5dd + 00aeee6 commit 6f6d832

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

core/src/array/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ impl<T: Ord, const N: usize> Ord for [T; N] {
330330
}
331331
}
332332

333+
#[cfg(not(bootstrap))]
334+
#[stable(feature = "copy_clone_array_lib", since = "1.58.0")]
335+
impl<T: Copy, const N: usize> Copy for [T; N] {}
336+
337+
#[cfg(not(bootstrap))]
338+
#[stable(feature = "copy_clone_array_lib", since = "1.58.0")]
339+
impl<T: Clone, const N: usize> Clone for [T; N] {
340+
#[inline]
341+
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()) }
345+
}
346+
347+
#[inline]
348+
fn clone_from(&mut self, other: &Self) {
349+
self.clone_from_slice(other);
350+
}
351+
}
352+
333353
// The Default impls cannot be done with const generics because `[T; 0]` doesn't
334354
// require Default to be implemented, and having different impl blocks for
335355
// different numbers isn't supported yet.

core/src/clone.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
///
9494
/// * Function item types (i.e., the distinct types defined for each function)
9595
/// * Function pointer types (e.g., `fn() -> i32`)
96-
/// * Array types, for all sizes, if the item type also implements `Clone` (e.g., `[i32; 123456]`)
9796
/// * Tuple types, if each component also implements `Clone` (e.g., `()`, `(i32, bool)`)
9897
/// * Closure types, if they capture no value from the environment
9998
/// or if all such captured values implement `Clone` themselves.

core/src/marker.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ pub trait StructuralEq {
359359
///
360360
/// * Function item types (i.e., the distinct types defined for each function)
361361
/// * Function pointer types (e.g., `fn() -> i32`)
362-
/// * Array types, for all sizes, if the item type also implements `Copy` (e.g., `[i32; 123456]`)
363362
/// * Tuple types, if each component also implements `Copy` (e.g., `()`, `(i32, bool)`)
364363
/// * Closure types, if they capture no value from the environment
365364
/// or if all such captured values implement `Copy` themselves.

0 commit comments

Comments
 (0)