Skip to content

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

Merged
merged 4 commits into from
May 11, 2018

Conversation

pitdicker
Copy link
Contributor

@pitdicker pitdicker commented May 9, 2018

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 and UniformWrapping, 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 to Wrapping types. So the code is now just mostly duplicated.

Fixes #168.

@dhardy
Copy link
Member

dhardy commented May 10, 2018

Okay, I get the motivation for implementing Standard, but not Uniform. Good work, but I'd like to get the next release out rather than keep adding features!

@vks want to review?

@pitdicker
Copy link
Contributor Author

pitdicker commented May 10, 2018 via email

Copy link
Member

@dhardy dhardy left a 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> {
Copy link
Member

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

@@ -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`");
Copy link
Member

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

@pitdicker
Copy link
Contributor Author

Removed the last commit, and edited two others as you commented on.

The commit implementing UniformSampler for Wrapping<T> is now in https://github.com/pitdicker/rand/commits/wrapping_impls_bak.

Copy link
Member

@dhardy dhardy left a 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 👍

@pitdicker pitdicker merged commit b1c3585 into rust-random:master May 11, 2018
@pitdicker pitdicker deleted the wrapping_impls branch May 11, 2018 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants