Skip to content

core: make the Rng a by-value parameter of Distribution::sample - #160205

Closed
joboet wants to merge 1 commit into
rust-lang:mainfrom
joboet:rng_by_val
Closed

core: make the Rng a by-value parameter of Distribution::sample#160205
joboet wants to merge 1 commit into
rust-lang:mainfrom
joboet:rng_by_val

Conversation

@joboet

@joboet joboet commented Jul 30, 2026

Copy link
Copy Markdown
Member

Given that &mut R now implements Rng if R does (#159435), I think it makes more sense for Distribution::sample to take the Rng by-value instead of by-ref. For stateless zero-sized types like SystemRng this is more efficient and convenient, and for everything else creating a reference still works just the same.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 30, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 30, 2026
@rustbot

rustbot commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from 6 candidates

@joboet joboet added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-random Area: random data generation support labels Jul 30, 2026
@tbu-

tbu- commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

For stateless zero-sized types like SystemRng this is more efficient and convenient, and for everything else creating a reference still works just the same.

Note that this PR can result in the exact thing it wants to prevent — more complex implementations of Distribution (that need to sample the RNG more than once), will need to create a reference. Chain that through multiple indirections (e.g. a struct containing a struct containing a struct) and you suddenly have &mut &mut &mut SystemRng. Before this PR, you still had &mut SystemRng, strictly better.

@joboet

joboet commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

That's a very good point. I'll nominate for libs-api to make sure using by-ref is an explicit decision.

@rustbot label +I-libs-api-nominated

@rustbot rustbot added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jul 30, 2026
@joshtriplett

joshtriplett commented Aug 4, 2026

Copy link
Copy Markdown
Member

We talked about this in today's @rust-lang/libs-api meeting.

We felt that the hazard of ending up with an &mut &mut SomeRng was unpleasant, and in practice the only case that gets a notable ergonomic improvement is SystemRng. It doesn't seem to make things substantively easier in other cases. Given that, we'd prefer not to do this.

To make the SystemRng case (and other cases) simpler, we'd prefer to consider a final fn random on Rng that takes a Distribution, so that you can call SystemRng.random(1..=6) which is nicer than (1..=6).sample(&mut SystemRng).

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-random Area: random data generation support I-libs-api-nominated Nominated for discussion during a libs-api team meeting. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants