Skip to content

RFC: Saturating and checking integer wrapper types - #1534

Closed
m4rw3r wants to merge 5 commits into
rust-lang:masterfrom
m4rw3r:master
Closed

m4rw3r wants to merge 5 commits into
rust-lang:masterfrom
m4rw3r:master

Conversation

@m4rw3r

@m4rw3r m4rw3r commented Mar 7, 2016

Copy link
Copy Markdown

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

@SoniEx2

SoniEx2 commented Mar 7, 2016

Copy link
Copy Markdown

Doesn't borrow ideas from #1530 ?

@m4rw3r

m4rw3r commented Mar 7, 2016

Copy link
Copy Markdown
Author

@SoniEx2 This is just the wrapper-types themselves, not how they are implemented (eg. the proposed Wrapping* traits from #1530 ). I try to avoid the issue about specific operators since that has been brought up before and would probably become the main point people would bikeshed about if it was included in the RFC.

As for traits to identify if a type is wrapping or not, that could be put in the num crate since it has other traits which allow you to be generic over different integer types.

@alexcrichton alexcrichton added the T-libs-api [DEPRECATED; DO NOT USE] label Mar 7, 2016

* `/` 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`.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(#1276)

Copy link
Copy Markdown
Author

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.

@comex

comex commented Mar 15, 2016

Copy link
Copy Markdown

👍 on changing the name to avoid confusion with checked_*.

@Zoxc

Zoxc commented Apr 3, 2016

Copy link
Copy Markdown

I want a Checked type which gives None if you get an overflow. This would correspond to the checked_* methods and probably give more efficient code than the Checked type proposed here anyway.

@brendanzab

Copy link
Copy Markdown

I would prefer to get an Option back too. Might I suggest making a proof-of-concept crate?

@aturon aturon self-assigned this May 9, 2016
@alexcrichton

alexcrichton commented Aug 23, 2016 •

Copy link
Copy Markdown
Member

🔔 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:

@alexcrichton alexcrichton added the final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. label Aug 23, 2016
@alexcrichton

Copy link
Copy Markdown
Member

Ok, the libs team has decided to close, so closing. Thanks regardless though for the RFC @m4rw3r!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. T-libs-api [DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants