Skip to content

Implement Standard and Uniform for Wrapping<T>#436

Merged
pitdicker merged 4 commits into
rust-random:masterfrom
pitdicker:wrapping_impls
May 11, 2018
Merged

Implement Standard and Uniform for Wrapping<T>#436
pitdicker merged 4 commits into
rust-random:masterfrom
pitdicker:wrapping_impls

Conversation

@pitdicker

@pitdicker pitdicker commented May 9, 2018

Copy link
Copy Markdown
Contributor

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

dhardy commented May 10, 2018

Copy link
Copy Markdown
Member

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

pitdicker commented May 10, 2018 via email

Copy link
Copy Markdown
Contributor Author

@dhardy dhardy left a comment

Copy link
Copy Markdown
Member

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.

}
}

impl<T> Distribution<Wrapping<T>> for Standard where Standard: Distribution<T> {

Copy link
Copy Markdown
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

Comment thread src/distributions/uniform.rs Outdated
// 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
Copy Markdown
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
Copy Markdown
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.

@dhardy dhardy left a comment

Copy link
Copy Markdown
Member

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