Skip to content
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

Add clamp function #262

Merged
merged 8 commits into from
Feb 9, 2017
Merged

Add clamp function #262

merged 8 commits into from
Feb 9, 2017

Conversation

Xaeroxe
Copy link
Contributor

@Xaeroxe Xaeroxe commented Feb 7, 2017

This PR adds a "clamp" function to num. Oftentimes it is desirable to restrict a value to between a certain minimum and maximum, the clamp function provides a generic way to do this.

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Feb 7, 2017

The build fails before it even starts compiling num. The build server is likely misconfigured.

@cuviper
Copy link
Member

cuviper commented Feb 7, 2017

The 1.0 failure is #257 -- nothing you can can fix here.

I'll give a review here in a little bit.

@clarfonthey
Copy link

Why does this require Copy? It doesn't seem necessary.

src/lib.rs Outdated
/// If input is less than min then min is returned, if input is greater than max then max is
/// returned. Otherwise input is returned.
#[inline]
pub fn clamp<T: PartialOrd + Copy>(input: T, min: T, max: T) -> T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @clarcharr that this shouldn't need Copy.

src/lib.rs Outdated
/// returned. Otherwise input is returned.
#[inline]
pub fn clamp<T: PartialOrd + Copy>(input: T, min: T, max: T) -> T {
debug_assert!(min < max, "min must be less than max");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think min <= max is fine, if we're going to assert anything.

src/lib.rs Outdated
debug_assert!(min < max, "min must be less than max");
if input <= min {min}
else if input >= max {max}
else {input}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting is non-standard -- please run it through rustfmt.

I think it would be slightly preferable to return input when it is equal to the boundaries. This only matters for cases similar to stable sorting, where there may be internal data that's not part of the ordering. So just compare input < min and input > max.

Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits -- sorry I failed to batch the GitHub review.

Also, we've moved away from any real functionality in num itself. I guess this should go into num-traits.

It needs at least some basic tests too.

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Feb 7, 2017

Yeah good point. Copy made sense at the time but it's not really needed.

Requested changes have been made.

@clarfonthey
Copy link

Another thing is that it'd be best to match on Ord::cmp instead of doing ifs.

@cuviper
Copy link
Member

cuviper commented Feb 7, 2017

Another thing is that it'd be best to match on Ord::cmp instead of doing ifs.

Why? Then it will require Ord too.

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Feb 7, 2017

The alternative is partial_cmp but I don't understand what benefit we gain from using that.

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Feb 7, 2017

use std::cmp::Ordering;
match input.partial_cmp(&min) {
    Some(Ordering::Less) => min,
    _ => 
        match input.partial_cmp(&max) {
            Some(Ordering::Greater) => max,
            _ => input
        },
}

This is my best attempt at implementing it using partial_cmp. I don't really like it.

Make assert debug error more accurate.
@cuviper
Copy link
Member

cuviper commented Feb 8, 2017

@clarcharr See rust-lang/rust#39538, which found in that case that binary comparisons perform better than a ternary cmp. Unless you can justify your suggestion further, I'm inclined to merge this as-is.

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Feb 8, 2017

The suggestion would make sense if it were possible to implement it using just one match statement, but because it is being compared to two values, not one, the suggestion doesn't really work.

Remove passive voice from documentation comments.
@cuviper cuviper merged commit d561e4b into rust-num:master Feb 9, 2017
@cuviper
Copy link
Member

cuviper commented Feb 9, 2017

Merged - thanks!

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.

3 participants