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

Use const-generics to support arrays of all sizes #1104

Merged
merged 6 commits into from Mar 25, 2021
Merged

Use const-generics to support arrays of all sizes #1104

merged 6 commits into from Mar 25, 2021

Conversation

noslaver
Copy link
Contributor

@noslaver noslaver commented Mar 13, 2021

As discussed in #1085, this PR uses min_const_generics to add support for random arrays of every size.
Currently this uses the existing nightly feature.
This currently updates the trait bounds on the implementation to require Default + Copy, which wasn't required previously, which is a regression compared to what is currently supported.
This can either be addressed by using unsafe code and MaybeUninit, or adding this new impl only for arrays larger than 32.

Open questions:

  • Should this use a new feature flag?
  • Any specific tests that should be added?
  • Any documentation that needs to be updated?

@newpavlov
Copy link
Member

@dhardy
Since the added code will soon work on stable, maybe we should introduce a separate feature (e.g. min_const_gen) instead of reusing the nightly one? Also personally I think it's fine to use MaybeUninit-based code to remove the T: Default + Copy bound.

@dhardy
Copy link
Member

dhardy commented Mar 14, 2021

Once min-const-generics is stable we'll want a feature flag to opt-in to this behaviour (until bumping the MSRV), so we should add the feature now, yes.

Yes, we should just use MaybeUninit now then.

Any documentation that needs to be updated?

  • Rng::gen
  • rand::distributions::Standard
  • Book: distributions
  • Use minimal const generics for supporting arrays of all sizes #1085 mentions let bytes: [u8; 4096] = rand::random(); which might actually be a bad idea for two reasons:
    1. naively, this requires copying a 4kiB block out of the generator function (and wrapping functions) instead of merely passing a slice reference in (whether this is optimised may also depend on use of #[inline])
    2. the current implementation of gen for arrays uses rng.gen() for each element which is inefficient for values smaller than 32-bit; I think we can't fix this unless either we require [T]: Fill and T: Default (a regression) or we have specialisation (maybe min_specialization is enough; we'd also have to think about whether we want specialisation to enable value-breaking changes).

@dhardy
Copy link
Member

dhardy commented Mar 20, 2021

I think this is mainly waiting on doc updates then?

@noslaver noslaver marked this pull request as ready for review March 20, 2021 09:44
@noslaver
Copy link
Contributor Author

I think this is mainly waiting on doc updates then?

Yes sorry for the delay.

Cargo.toml Outdated Show resolved Hide resolved
Copy link
Collaborator

@vks vks left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

One more bit of doc and I think we're good to go.

src/distributions/mod.rs Show resolved Hide resolved
@dhardy dhardy merged commit 9555338 into rust-random:master Mar 25, 2021
@noslaver noslaver deleted the const-generic-array-size branch March 25, 2021 19:25
dhardy added a commit to dhardy/rand that referenced this pull request Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants