-
-
Notifications
You must be signed in to change notification settings - Fork 464
Implement Standard
and Uniform
for Wrapping<T>
#436
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
Conversation
Okay, I get the motivation for implementing @vks want to review? |
Yes, I don't mind if this PR remains open for a while. But the commit that
moves the bound checks would be good to get in, as it adds an additional
requirement for implementers. Shall I split it out?
|
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.
The first four commits are fine; you could repost the last commit (or two if prefered) in another PR if you like.
Shame the wrapping impl needs a lot more code.
@@ -161,6 +162,13 @@ impl<T> Distribution<Option<T>> for Standard where Standard: Distribution<T> { | |||
} | |||
} | |||
|
|||
impl<T> Distribution<Wrapping<T>> for Standard where Standard: Distribution<T> { |
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.
Mention in the Standard
doc
src/distributions/uniform.rs
Outdated
@@ -322,12 +320,16 @@ macro_rules! uniform_int_impl { | |||
#[inline] // if the range is constant, this helps LLVM to do the | |||
// calculations at compile-time. | |||
fn new(low: Self::X, high: Self::X) -> Self { | |||
assert!(low < high, | |||
"Uniform::new_inclusive called with `low >= high`"); |
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.
This isn't new_inclusive
Removed the last commit, and edited two others as you commented on. The commit implementing |
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.
Then this is good I think 👍
This turned out to be a bit messier than I hoped...
The range checks are now moved to the
UniformSampler
implementations, as discussed in #433 (comment).For wrapping types I wanted to have the range wrap around if
low > high
. This turned out to need a slightly different way to calculate the range and zone. Because it is simpler, I also adjusted the normal integer implementation.I tried to share the code between
UniformInt
andUniformWrapping
, but didn't find a nice way. One option is to abstract things away into another trait (pitdicker@6c21536), which is ugly. Implementing it directly by changing the macro was also not easy, because of the converting back-and-forth toWrapping
types. So the code is now just mostly duplicated.Fixes #168.