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

libstd: Use std::array::from_fn to implement Default for arrays larger than 32 elements. #124163

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 5 additions & 22 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,30 +428,13 @@ impl<T: Copy> 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.
Comment on lines -431 to -433
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this resolved?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... I totally misread that, ugh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I could fix this by using a conditional impl using generic_const_exprs, but that causes a bunch of other code in libcore to fail to build, see #124167

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other clever way of getting this to work out of curiosity? (Not familiar with the specialisation features that are available in nightly)

Copy link
Member

@Nilstrieb Nilstrieb Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generic_const_exprs is very much forbidden from being used on the standard library, no matter this issue. it's way too broken for that.


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()),*]
}
}
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] { [] }
}
};
#[stable(feature = "default_array_lib", since = "1.79.0")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is the right thing to do annotation wise.

impl<T, const N: usize> Default for [T; N] where T: Default {
fn default() -> [T; N] {
from_fn::<T, N, _>(|_| 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, const N: usize> [T; N] {
/// Returns an array of the same size as `self`, with function `f` applied to each element
/// in order.
Expand Down
5 changes: 1 addition & 4 deletions library/core/src/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,16 +589,13 @@ mod prim_pointer {}
/// - [`Copy`]
/// - [`Clone`]
/// - [`Debug`]
/// - [`Default`]
/// - [`IntoIterator`] (implemented for `[T; N]`, `&[T; N]` and `&mut [T; N]`)
/// - [`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`]
/// - [`Hash`]
/// - [`AsRef`], [`AsMut`]
/// - [`Borrow`], [`BorrowMut`]
///
/// Arrays of sizes from 0 to 32 (inclusive) implement the [`Default`] trait
/// if the element type allows it. As a stopgap, trait implementations are
/// statically generated up to size 32.
///
/// Arrays of sizes from 1 to 12 (inclusive) implement [`From<Tuple>`], where `Tuple`
/// is a homogeneous [prim@tuple] of appropriate length.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ pub fn yes_ord() -> impl Ord {
[0; 32]
}

pub fn yes_default() -> impl Default {
[0; 32]
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ pub fn yes_ord() -> impl Ord {
[0; 33]
}

pub fn yes_default() -> impl Default {
[0; 33]
}

fn main() {}