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

Add Default support for arrays of all sizes #153

Closed
saeubank opened this issue Dec 25, 2022 · 4 comments
Closed

Add Default support for arrays of all sizes #153

saeubank opened this issue Dec 25, 2022 · 4 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@saeubank
Copy link

Proposal

Problem statement

Arrays over the size of 32 do not support Default.

Motivation, use-cases

let arr = <[u8; 64]>::default();

Solution sketches

[T::default(); N]
This does not work because default only gets called once.
If for some reason an implementation of default is like a generator, this will not behave as expected since the whole array will be the same value rather than being the generated value.

Links and related work

rust-lang/rust#106145

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.

@saeubank saeubank added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Dec 25, 2022
@cuviper
Copy link
Member

cuviper commented Dec 25, 2022

We can't do this until we have a solution for [T; 0] without T: Default, or a convincing argument to make that breaking change.

@saeubank
Copy link
Author

Yeah it looks like it will be a breaking change until negative Traits are implemented.

impl<T> Default for [T; 0] where 
 T: !Default {
    fn default() -> Self {
        []
    }
}

I think that this is worth making a breaking change for.
Is it worth using @craterbot to check if people actually use default for [T; 0] where T does not implement Default?

@cuviper
Copy link
Member

cuviper commented Dec 26, 2022

I didn't have the issue at hand before, but that's rust-lang/rust#61415 for a lot more history.

@dtolnay
Copy link
Member

dtolnay commented Dec 26, 2022

I'm confident the library API team is on board with arbitrarily large [T; N] implementing Default. The missing piece is just figuring out a way to implement it — as mentioned above, this is tracked in rust-lang/rust#61415.

@dtolnay dtolnay closed this as completed Dec 26, 2022
@dtolnay dtolnay added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants