-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC: Saturating and checking integer wrapper types #1534
Conversation
Doesn't borrow ideas from #1530 ? |
@SoniEx2 This is just the wrapper-types themselves, not how they are implemented (eg. the proposed As for traits to identify if a type is wrapping or not, that could be put in the |
|
||
* `/` and `%` cannot overflow on unsigned integers. For signed integers `MIN / -1` and `MIN % -1` | ||
can overflow since signed types follow two's complement: `-1 * MIN = MAX + 1` and `MIN` is | ||
always even. The proposed results are `MIN / -1 = MAX` and `MIN % -1 = MAX`. |
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.
MIN % -1
is well defined as 0
. IIRC the only reason it's not this way in Rust is that it requires an additional branch for every op, but that's required here anyway.
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.
(#1276)
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.
Good point. Changing to 0
for MIN % -1
.
👍 on changing the name to avoid confusion with |
I want a |
I would prefer to get an |
🔔 This RFC is now entering its final comment period 🔔 The @rust-lang/libs team is inclined to close this RFC. It's unfortunately been inactive for quite some time now and it seems that the desire here may not be enough to move into the standard library yet. It would likely be worth prototyping this on crates.io and seeing how it plays out there before moving. @rust-lang/libs if you could check off your name below or write a comment below to the contrary: |
Ok, the libs team has decided to close, so closing. Thanks regardless though for the RFC @m4rw3r! |
Implement two wrapper-types in
std::num
which provide two different types of behavior on overflow: saturating arithmetic and always checked arithmetic with the latter signalling a thread panic on overflow.Rendered