-
Notifications
You must be signed in to change notification settings - Fork 430
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 trait SampleRng: Rng and related doc #244
Conversation
Wacky idea: we could rename This would provide a nice split between front-end and back-end functionality, but I think overall would be confusing and not so useful (I generally disapprove of using the same name for different things in different namespaces). As I said, wacky idea. |
Is that helpful? Afaik, specialization cannot violate crate boundaries, right? I.e. Afaik, there are no sensible arguments why separating There might be arguments in favor of a slimmer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After seeing your commit, but before looking at it closely I thought the extra trait provides a nice separation between front- and back-end. There really is a difference between the methods that make sense for an RNG to implement, and the methods that are best for user code.
I would also like to see rand-core
being separated out. We really should not go overboard and split rand
up into 10 tiny crates. But a core that is useful for implementing RNGs, and for crates that deal directly with RNGs, seems useful to me.
The changes and documentation look good to me.
Is SampleRng
really the nicest name we can come up with? The three most interesting methods in it are gen
, gen_iter
and gen_range
. Does it make sense to pick something with "gen" in the name, like GenRandom
? Although I do not care much for the trait name to be honest.
@burdges If the You do make two good arguments, boilerplate and documentation. I think |
@burdges the wacky idea was two separate traits, both named If we were to move some distributions code out to I'm not convinced about whether or not we need a separate
There will also be a There have already been a couple of suggestions to add a |
Your statement that " I'd almost agree the separation stops being quite so harmful for documentation if users almost never want functions from Are we clear on the relationship between We must remember that If the Instead, if we have I still think even if crate separation works it brings only needless constraints and pain. It's easiest to design without crate separation and then ask if your design splits cleanly afterwards. |
If documentation requires clicking through to the source, that's a deficiency in the documentation, in my opinion. That does sometimes happen but I'm not sure it's something we need to design around?
That's what's in this PR (plus the "wacky renames"), yes. I see your point about I'm not really sure whether users should prefer to use (core)
It fits the constraints we already have, except for this PR and the separate |
I tried adding a fn try_fill<T: AsByteSlice + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> { ... } and found:
The same method can be added to Does this count as motivation for this PR? Of course a generic |
My mistake, the above is because But this brings us to a more important point: generic methods like (Note: |
To confuse this topic even more, (&mut r).gen::<i32>(); I don't understand how this works; presumably it forces r.gen::<i32>();
However, this syntax does work with this PR (also in my |
All methods already require the bound
After some more investigation, I finally understand how impl<'a, R: ?Sized> Rng for &'a mut R where R: Rng { ... } Which can be specialised as: impl<'a> Rng for &'a mut Rng { ... } This provides a Note that we could change Conclusion: if we do not wish to add this extension trait, it appears reasonable to forgo direct support for dynamic dispatch on |
Rebasing #256 without this PR is very easy. So the question here appears to be is it worth adding an extension trait (a) to simplify syntax with dynamic dispatch and (b) to allow I think I agree with @burdges now that unless we have a real need for a separate |
One reason to split out a |
A stable back-end would allow stable PRNG/entropy source crates to be published sooner — but most consumers are indeed on the front-end. In reply to your other post about Serde's type erasure, I don't think adding an |
I think |
That's a moot point because |
I'm now tempted to rename I still like the idea of an extension trait because it separates what RNGs are supposed to implement and what they are not (e.g. backends should not be modifying |
Hmm, it's true Bikeshedding the name aside I think the trait separation is fine too because it simplifies the mechanics of the |
Oh, good, sounds like we have mostly positive support for this change. To go back to the bike-shedding, I'm tempted to go with @newpavlov's suggestion: |
As far as colours for bikesheds go, |
Then I will close in favour of #265. |
Move high-level
Rng
functionality to an extension trait. See documentation here.This change is necessary to keep a similar interface for users while allowing a separate
rand-core
crate. Note that users often won't need to importRng
at all, as many of the examples and theseq
module show.Related:
Sample
, but usingSampleRng
is consistent with other extension-traits and perhaps less confusingnext_f32
andnext_f64
fromRng
. That may or may not happen; it's not necessary for this change or therand-core
split, and those are definitely low-level methods.Rand
,Sample
andIndepedentSample
all useRng
, notSampleRng
still. That could change but would be a breaking change (since all the above may be replaced by a singleDistribution
trait, we can make the change then). I'm not certain which is best; possiblySampleRng
since this eliminates the need to importRng
in many cases?Also, @burdges said recently he was against this trait and the
rand-core
split. I don't think usage will be much different for end users (if anything, documentation is improved by separating front- and back-end functionality) and several people have requestedrand-core
so I think overall this is a win.