-
Notifications
You must be signed in to change notification settings - Fork 432
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
Distributions without direct dynamic dispatch #261
Conversation
This is heavily inspired by #27 by @GrahamDennis but simpler trait and maintains backwards compatibility with deprecations.
Replaces rng.gen() to seed new RNGs with from_rng(&mut rng)
Move default sampling to Default distribution Make Open01 and Closed01 distributions instead of wrappers
…erive. This breaks rand_derive because gen() no longer supports derived types.
if mem::size_of::<isize>() == 4 { | ||
rng.gen::<i32>() as isize | ||
} else { | ||
rng.gen::<i64>() as isize |
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.
What about platforms with 16 bit pointers? Does Rust support them?
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.
I don't think so. There was some discussion of 16-bit CPUs here but ultimately because of the extremely limited memory addressing I think most 16-bit code has to be written expressly for that architecture.
Looks good to me!
Could you give some example user code contrasting the two approaches? |
With this PR: use rand::{thread_rng, Rng};
use rand::distributions::Uniform;
let mut rng = thread_rng();
let mut erased_rng: &mut Rng = &mut rng;
let val: f32 = (&mut erased_rng).sample(Uniform);
println!("f32 from [0,1): {}", val); With #256 instead: use rand::{thread_rng, Rng, SampleRng};
use rand::distributions::Uniform;
let mut rng = thread_rng();
let mut erased_rng: &mut Rng = &mut rng;
let val: f32 = erased_rng.sample(Uniform);
println!("f32 from [0,1): {}", val); As you can see, very little difference — the extra reference to |
Thanks! Why is the extra Edit: I missed what you said in #244:
|
See this comment. From the error message one wouldn't expect to be able to use generic methods on a type-erased
|
If I understand it correctly, this is caused by this: impl<'a> Rng for &'a mut Rng { ... } Why is this |
Without that impl, the indirect dispatch |
Also, one writes |
This is obsolete now that I've merged #265. |
This is #256 without #244, i.e.
Distribution::sample
takesrng: &mut R where R: Rng
, notR: Rng + ?Sized
.The advantage of this approach is that it doesn't require #244, which in some ways is less ergonomic for users.
The disadvantages are
Distribution
implementations