std: simplify the random feature#157573
Conversation
|
r? @clarfonthey rustbot has assigned @clarfonthey. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
I would also prefer something like this over trying to stabilize a trait for random sources as well. Even if we take as given that std will grow such a trait eventually, I think I have not previously advocated for including |
| #[unstable(feature = "random", issue = "130703", reason = "outstanding API concerns")] | ||
| pub trait Distribution<T>: private::Sealed<T> { | ||
| /// Samples a random value from the distribution. | ||
| fn sample(&self) -> T; |
There was a problem hiding this comment.
Nit: if we want to stabilize fn random without committing to this method signature, it's not enough to keep Distribution sealed, the method also has to be unstable. Or we could keep Distribution unstable entirely, but in that case we don't strictly need to seal it.
|
I should say that this PR implements the most drastic version of this entire thing. It's totally reasonable to just add |
Tracking issue: #130703
This PR is a proposal to resolve all of the concerns around the
randomfeature by removing the most controversial parts in a future-compatible fashion.RandomSourceThe main part of this proposal concerns
RandomSource/Rng: I think it should be removed entirely, at least for the time being.Random number generators are used in highly different scenarios with different requirements:
Trying to fulfil both needs with a single API leads to problems like whether to provide generator methods for common numeric types (#157193), which endianness to choose when constructing those types from a byte buffer (#157430). I do not know whether a one-size-fits-all-solution to these kind of problems exists, but past evidence from the
randcrate indicates that this requires loads of experimentation. In my opinion,stdisn't a good place for that.Thus this PR removes
RandomSourceandDefaultRandomSourcein favour of a single function:This is equivalent to the previous
DefaultRandomSource::fill_bytesmethod (I've left the choice of entropy sources unchanged). Should we, at some point, reintroduceRandomSource, this method is still very useful as a shorthand, so this is entirely future-compatible.Having such an API in
stdin general is highly desired and generally uncontroversial, with the exception of three issues:Pluggability can be left for future consideration: once EIIs are a thing this will become much easier. The same applies to uninitialised buffers: even if we add a
fill_buffunction takingBorrowedCursorlater, the standardfillfunction will remain useful. Fallability is a more difficult question though:Fallibility
Contrary to the
getrandomcrate,std::random::filldoes not return an error. Random number generation is infallible on nearly all targets supported bystd:getrandommight block for a long time, but does not fail for reasons that cannot be handled bystd.ProcessRngalways succeeds.CCRandomGenerateBytescan theoretically fail, but does not in practise.arc4random_bufAPI used on most UNIXes cannot return an error.cprng_drawAnd in general, if there is at least some source of entropy on the platform, waiting until enough entropy has been acquired to seed a CSPRNG is always an option. The only problematic platform I've encountered so far is ESP-IDF which does have an RNG but does not have a way of confirming the quality of it (see #157566). I don't think we should adjust the API to work around these weird platforms if nearly every other one does not have issues.
Thus
std::random::fillis kept infallible.DistributionProviding
std::random::fillalone provides the risk that users will attempt to use hacky, incorrect ways of generating numbers in a specific range. Thus, this PR preserves theDistributiontrait and therandomfunction to provide an easy, correct way of sampling in a restricted range. While this PR does not implementDistributionfor types other thanRangeFull, I think an implementation for other ranges should be merged before stabilisation.stdwould be in good company with this API: e.g. most UNIXes havearc4randomandarc4random_uniformfor this kind of sampling alongsidearc4random_buf.In order to leave room for readding a
RandomSource/Rngin the future,Distributionis marked as sealed for the time being. This still permits all the simple uses for numeric types.