Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default for arrays via const generics #74254

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,29 +353,57 @@ impl<T: Ord, const N: usize> Ord for [T; N] {
}
}

// The Default impls cannot be generated using the array_impls! macro because
// they require array literals.

macro_rules! array_impl_default {
{$n:expr, $t:ident $($ts:ident)*} => {
#[stable(since = "1.4.0", feature = "array_default")]
impl<T> Default for [T; $n] where T: Default {
fn default() -> [T; $n] {
[$t::default(), $($ts::default()),*]
}
/// This module implements `Default` for arrays.
mod default_impls {
// A trait implemented by all arrays which are either empty or contain a type implementing `Default`.
#[unstable(
feature = "array_default_internals",
reason = "implementation detail",
issue = "none"
)]
#[marker]
pub trait ArrayDefault {}

#[unstable(
feature = "array_default_internals",
reason = "implementation detail",
issue = "none"
)]
impl<T> ArrayDefault for [T; 0] {}

#[unstable(
feature = "array_default_internals",
reason = "implementation detail",
issue = "none"
)]
impl<T: Default, const N: usize> ArrayDefault for [T; N] {}

trait DefaultHack {
fn default_hack() -> Self;
}

impl<T> DefaultHack for T {
default fn default_hack() -> Self {
unreachable!();
}
array_impl_default!{($n - 1), $($ts)*}
};
{$n:expr,} => {
#[stable(since = "1.4.0", feature = "array_default")]
impl<T> Default for [T; $n] {
fn default() -> [T; $n] { [] }
}

impl<T: Default> DefaultHack for T {
fn default_hack() -> Self {
Default::default()
}
};
}
#[stable(since = "1.4.0", feature = "array_default")]
impl<T, const N: usize> Default for [T; N]
where
[T; N]: ArrayDefault,
{
fn default() -> [T; N] {
[(); N].map(|_unit| DefaultHack::default_hack())
}
}
}

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}

#[lang = "array"]
impl<T, const N: usize> [T; N] {
/// Returns an array of the same size as `self`, with function `f` applied to each element
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
// specialization is used in array impls
#[rustc_specialization_trait]
pub trait Default: Sized {
/// Returns the "default value" for a type.
///
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#![allow(explicit_outlives_requirements)]
#![allow(incomplete_features)]
#![feature(allow_internal_unstable)]
#![cfg_attr(not(bootstrap), feature(array_default_internals))]
#![feature(arbitrary_self_types)]
#![feature(asm)]
#![feature(cfg_target_has_atomic)]
Expand Down Expand Up @@ -111,6 +112,7 @@
#![feature(lang_items)]
#![feature(link_llvm_intrinsics)]
#![feature(llvm_asm)]
#![feature(marker_trait_attr)]
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(nll)]
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ fn array_default_impl_avoids_leaks_on_panic() {
}
}

let res = std::panic::catch_unwind(|| <[Bomb; 5]>::default());
let res = std::panic::catch_unwind(|| <[Bomb; 35]>::default());
let panic_msg = match res {
Ok(_) => unreachable!(),
Err(p) => p.downcast::<&'static str>().unwrap(),
Expand Down