Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd clamp function #262
Conversation
Xaeroxe
added some commits
Feb 7, 2017
This comment has been minimized.
This comment has been minimized.
|
The build fails before it even starts compiling num. The build server is likely misconfigured. |
This comment has been minimized.
This comment has been minimized.
|
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
This comment has been minimized.
This comment has been minimized.
clarfon
commented
Feb 7, 2017
|
Why does this require |
cuviper
reviewed
Feb 7, 2017
| /// 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.
This comment has been minimized.
cuviper
reviewed
Feb 7, 2017
| /// 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.
This comment has been minimized.
cuviper
reviewed
Feb 7, 2017
| 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.
This comment has been minimized.
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
requested changes
Feb 7, 2017
|
A few nits -- sorry I failed to batch the GitHub review. Also, we've moved away from any real functionality in It needs at least some basic tests too. |
This comment has been minimized.
This comment has been minimized.
|
Yeah good point. Copy made sense at the time but it's not really needed. Requested changes have been made. |
This comment has been minimized.
This comment has been minimized.
clarfon
commented
Feb 7, 2017
|
Another thing is that it'd be best to match on |
This comment has been minimized.
This comment has been minimized.
Why? Then it will require |
This comment has been minimized.
This comment has been minimized.
|
The alternative is partial_cmp but I don't understand what benefit we gain from using that. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
|
@clarcharr See rust-lang/rust#39538, which found in that case that binary comparisons perform better than a ternary |
This comment has been minimized.
This comment has been minimized.
|
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. |
cuviper
merged commit d561e4b
into
rust-num:master
Feb 9, 2017
1 check failed
This comment has been minimized.
This comment has been minimized.
|
Merged - thanks! |
Xaeroxe commentedFeb 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.