You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Working with ?Sized bounds when implementing Distribution #725
Say I have a struct Foo(String); and I want the Standard distribution for Foo to output values with 5-character alphanumeric strings. So I'm trying this:
Well... This doesn't work, since sample_iter requires R to be Sized. I also can't remove the ?Sized bound from the impl, because then the compiler complains that it is more specific than the trait.
The only workaround I managed to come up with is this:
It's probably a matter of personal taste, but to me rng.sample_iter(distr).take(x) looks much cleaner than (0..x).map(|_| distr.sample(rng)) - it just reads better, "I want a stream of values with such and such distribution and take x of them" rather than "transform a range of values into values from such and such distribution". And an unused closure argument in map is pretty much a code smell to me... But as I said, this might be just my personal preference.
In any case, if the requirement for Sized can be dropped from sample_iter, it would be my preferred way forward.
Activity
dhardy commentedon Feb 5, 2019
I don't think there's any reason we can't correct
sample_iterto acceptR: ?Sized...however, this method isn't really necessary anyway: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4fb68589bbcc8715113be405c9e047ae
Any thoughts on whether we should keep it? Perhaps since discoverability is better.
fizyk20 commentedon Feb 6, 2019
It's probably a matter of personal taste, but to me
rng.sample_iter(distr).take(x)looks much cleaner than(0..x).map(|_| distr.sample(rng))- it just reads better, "I want a stream of values with such and such distribution and take x of them" rather than "transform a range of values into values from such and such distribution". And an unused closure argument inmapis pretty much a code smell to me... But as I said, this might be just my personal preference.In any case, if the requirement for
Sizedcan be dropped fromsample_iter, it would be my preferred way forward.vks commentedon Feb 6, 2019
Since this is not a breaking change, I agree that we should just do this. Whether or not
sample_iteris needed is orthogonal to this issue.dhardy commentedon Feb 15, 2019
Fix implemented in https://github.com/dhardy/rand/commits/sized . Needs to wait until 0.7 release.