Closed
Description
Feature gate: #![feature(mixed_integer_ops)]
Public API
// `uX` is `u8`, `u16`, `u32`, `u64`,`u128`, `usize`
impl uX {
pub const fn checked_add_signed(self, iX) -> Option<Self>;
pub const fn overflowing_add_signed(self, iX) -> (Self, bool);
pub const fn saturating_add_signed(self, iX) -> Self;
pub const fn wrapping_add_signed(self, iX) -> Self;
}
impl iX {
pub const fn checked_add_unsigned(self, uX) -> Option<Self>;
pub const fn overflowing_add_unsigned(self, uX) -> (Self, bool);
pub const fn saturating_add_unsigned(self, uX) -> Self;
pub const fn wrapping_add_unsigned(self, uX) -> Self;
pub const fn checked_sub_unsigned(self, uX) -> Option<Self>;
pub const fn overflowing_sub_unsigned(self, uX) -> (Self, bool);
pub const fn saturating_sub_unsigned(self, uX) -> Self;
pub const fn wrapping_sub_unsigned(self, uX) -> Self;
}
Steps / History
- Implementation: Add functions to add unsigned and signed integers #87601Final comment period (FCP)Stabilization PR: Stabilize
#![feature(mixed_integer_ops)]
#101555
Activity
leonardo-m commentedon Jan 26, 2022
Somewhere you should write about use cases & purposes of these ops :)
carrying_add
andborrowing_sub
for signed integers. #93873brendanzab commentedon Mar 10, 2022
I've needed
checked_add_signed
for working withio::SeekFrom
, for example! Had to roll my own (and I'm not sure how correct it is), inspired by this implementation:It would be great to have this in the stdlib!
a1phyr commentedon Mar 10, 2022
This is exactly what prompted me to write these !
Also it is what
std
uses forCursor
now.tjallingt commentedon Jun 7, 2022
Would love to see this get stabilized. Not sure what the right process is to start an FCP
signed_difference
jonhoo/hashbag#11avl commentedon Jul 23, 2022
I love these methods!
I would love to see *_sub_signed and friends too. I just had a use for saturating_sub_signed (subtracting a signed residual from an unsigned value to create a corrected value). In my case, I can just negate the residual. However, that might cause overflow, in which case the final calculated value would not be correct. This won't really matter in practice, but saturating_sub_signed still would have been nice!
joshtriplett commentedon Aug 10, 2022
I just ran into a use case for these as well, in Cargo, for working with CPU counts (e.g. "use all but 1 CPU").
joshtriplett commentedon Aug 10, 2022
These have been around for a long time, and it seems like several people are interested in them.
Shall we stabilize these?
@rfcbot merge
24 remaining items