From 051168f852a5fc5dafe25a24e51151e01475cc83 Mon Sep 17 00:00:00 2001 From: Speedy_Lex <78314533+speedy-lex@users.noreply.github.com> Date: Sun, 14 Sep 2025 02:58:00 +0200 Subject: [PATCH] test: make [T; N]::default use const generics --- library/core/src/array/mod.rs | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index d14419a23a1d8..45736279bc526 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -469,30 +469,13 @@ impl SpecArrayClone for T { } } -// 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. - -macro_rules! array_impl_default { - {$n:expr, $t:ident $($ts:ident)*} => { - #[stable(since = "1.4.0", feature = "array_default")] - impl Default for [T; $n] where T: Default { - fn default() -> [T; $n] { - [$t::default(), $($ts::default()),*] - } - } - array_impl_default!{($n - 1), $($ts)*} - }; - {$n:expr,} => { - #[stable(since = "1.4.0", feature = "array_default")] - impl Default for [T; $n] { - fn default() -> [T; $n] { [] } - } - }; +#[stable(since = "1.4.0", feature = "array_default")] +impl Default for [T; N] { + fn default() -> Self { + from_fn(|_| T::default()) + } } -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} - impl [T; N] { /// Returns an array of the same size as `self`, with function `f` applied to each element /// in order.