Skip to content

Commit 975a8ed

Browse files
committed
Implemented Default for arrays up to [T; 32].
1 parent c1e865c commit 975a8ed

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/libcore/array.rs

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use clone::Clone;
2222
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
2323
use convert::{AsRef, AsMut};
24+
use default::Default;
2425
use fmt;
2526
use hash::{Hash, self};
2627
use iter::IntoIterator;
@@ -161,3 +162,26 @@ array_impls! {
161162
20 21 22 23 24 25 26 27 28 29
162163
30 31 32
163164
}
165+
166+
// The Default impls cannot be generated using the array_impls! macro because
167+
// they require array literals.
168+
169+
macro_rules! array_impl_default {
170+
{$n:expr, $t:ident $($ts:ident)*} => {
171+
#[stable(since = "1.4.0", feature = "array_default")]
172+
impl<T> Default for [T; $n] where T: Default {
173+
fn default() -> [T; $n] {
174+
[$t::default(), $($ts::default()),*]
175+
}
176+
}
177+
array_impl_default!{($n - 1), $($ts)*}
178+
};
179+
{$n:expr,} => {
180+
#[stable(since = "1.4.0", feature = "array_default")]
181+
impl<T> Default for [T; $n] {
182+
fn default() -> [T; $n] { [] }
183+
}
184+
};
185+
}
186+
187+
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}

0 commit comments

Comments
 (0)