-
Notifications
You must be signed in to change notification settings - Fork 8
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
Clamp isize values returned by signed_difference
#11
Conversation
Since the difference of two usizes may not fit in an isize, clamp the values to isize::MIN/MAX when the difference cannot be represented. Also, be careful to avoid subtracting with overflow when computing the diff
Codecov Report
|
Rather than unit tests
I think you can do: if a > b {
isize::try_from(a - b).unwrap_or(isize::MAX)
} else {
isize::try_from(b - a).map(isize::saturaing_neg).unwrap_or(isize::MIN)
} It would be a lot nicer with rust-lang/rust#87840, but for now I think you need ^. |
Ah, yeah. I think even |
Make other code common Co-authored-by: Jon Gjengset <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Released as 0.1.9 🎉 |
Put 1.70 in there (for instance if you want to pin against OnceLock stabilizing) and it will actually test 1.7 as it appears github auto converts this to a float? Putting in quotes seems to do the right thing here
Since the difference of two usizes may not fit in an isize, clamp the
values to isize::MIN/MAX when the difference cannot be represented.
Also, be careful to avoid subtracting with overflow when computing the
diff
I couldn't think of a more succinct way to perform a clamping difference,
I'd be happy if there's a way to do it cleaner.