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

Conversation

Projects
None yet
3 participants
@Xaeroxe
Copy link
Contributor

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 added some commits Feb 7, 2017

@Xaeroxe

This comment has been minimized.

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

This comment has been minimized.

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.

Xaeroxe added some commits Feb 7, 2017

@clarfon

This comment has been minimized.

Copy link

clarfon commented Feb 7, 2017

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 {

This comment has been minimized.

@cuviper

cuviper Feb 7, 2017

Member

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");

This comment has been minimized.

@cuviper

cuviper Feb 7, 2017

Member

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}

This comment has been minimized.

@cuviper

cuviper Feb 7, 2017

Member

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.

@cuviper
Copy link
Member

cuviper left a comment

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

This comment has been minimized.

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.

@clarfon

This comment has been minimized.

Copy link

clarfon commented Feb 7, 2017

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

@cuviper

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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.

Update lib.rs
Make assert debug error more accurate.
@cuviper

This comment has been minimized.

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

This comment has been minimized.

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.

Improve documentation comments
Remove passive voice from documentation comments.

@cuviper cuviper merged commit d561e4b into rust-num:master Feb 9, 2017

1 check failed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
@cuviper

This comment has been minimized.

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
You can’t perform that action at this time.