Skip to content

Commit d64dbff

Browse files
committed
libstd: Use std::array::from_fn to implement Default for arrays larger than 32 elements.
Much like #74060 did for every other trait. This avoids tools like rust-bindgen having to painfully work around this limitations, sometimes incorrectly like in rust-lang/rust-bindgen#2803
1 parent 07d0d7c commit d64dbff

File tree

4 files changed

+13
-26
lines changed

4 files changed

+13
-26
lines changed

library/core/src/array/mod.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -428,30 +428,12 @@ impl<T: Copy> SpecArrayClone for T {
428428
}
429429
}
430430

431-
// The Default impls cannot be done with const generics because `[T; 0]` doesn't
432-
// require Default to be implemented, and having different impl blocks for
433-
// different numbers isn't supported yet.
434-
435-
macro_rules! array_impl_default {
436-
{$n:expr, $t:ident $($ts:ident)*} => {
437-
#[stable(since = "1.4.0", feature = "array_default")]
438-
impl<T> Default for [T; $n] where T: Default {
439-
fn default() -> [T; $n] {
440-
[$t::default(), $($ts::default()),*]
441-
}
442-
}
443-
array_impl_default!{($n - 1), $($ts)*}
444-
};
445-
{$n:expr,} => {
446-
#[stable(since = "1.4.0", feature = "array_default")]
447-
impl<T> Default for [T; $n] {
448-
fn default() -> [T; $n] { [] }
449-
}
450-
};
431+
impl<T, const N: usize> Default for [T; N] where T: Default {
432+
fn default() -> [T; N] {
433+
from_fn::<T, N, _>(|_| T::default())
434+
}
451435
}
452436

453-
array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T}
454-
455437
impl<T, const N: usize> [T; N] {
456438
/// Returns an array of the same size as `self`, with function `f` applied to each element
457439
/// in order.

library/core/src/primitive_docs.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -589,16 +589,13 @@ mod prim_pointer {}
589589
/// - [`Copy`]
590590
/// - [`Clone`]
591591
/// - [`Debug`]
592+
/// - [`Default`]
592593
/// - [`IntoIterator`] (implemented for `[T; N]`, `&[T; N]` and `&mut [T; N]`)
593594
/// - [`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`]
594595
/// - [`Hash`]
595596
/// - [`AsRef`], [`AsMut`]
596597
/// - [`Borrow`], [`BorrowMut`]
597598
///
598-
/// Arrays of sizes from 0 to 32 (inclusive) implement the [`Default`] trait
599-
/// if the element type allows it. As a stopgap, trait implementations are
600-
/// statically generated up to size 32.
601-
///
602599
/// Arrays of sizes from 1 to 12 (inclusive) implement [`From<Tuple>`], where `Tuple`
603600
/// is a homogeneous [prim@tuple] of appropriate length.
604601
///

tests/ui/const-generics/array-impls/core-traits-impls-length-32.rs

+4
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ pub fn yes_ord() -> impl Ord {
6363
[0; 32]
6464
}
6565

66+
pub fn yes_default() -> impl Default {
67+
[0; 32]
68+
}
69+
6670
fn main() {}

tests/ui/const-generics/array-impls/core-traits-impls-length-33.rs

+4
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ pub fn yes_ord() -> impl Ord {
6363
[0; 33]
6464
}
6565

66+
pub fn yes_default() -> impl Default {
67+
[0; 33]
68+
}
69+
6670
fn main() {}

0 commit comments

Comments
 (0)