]> git.lizzy.rs Git - rust.git/commit - src/tools/rust-analyzer
Rollup merge of #87601 - a1phyr:feature_uint_add_signed, r=kennytm
authorManish Goregaokar <manishsmail@gmail.com>
Wed, 6 Oct 2021 19:33:13 +0000 (12:33 -0700)
committerGitHub <noreply@github.com>
Wed, 6 Oct 2021 19:33:13 +0000 (12:33 -0700)
commit3209582a87f7b8e098bac67f66ed58d8d5840dee
treedd17f17506a04ccb9167e7d3b6b2bb8cbf1a6a86
parentd7539a6af09e5889ed9bcb8b49571b7a59c32e65
parent47edde1086412b36e9efd6098b191ec15a2a760a
Rollup merge of #87601 - a1phyr:feature_uint_add_signed, r=kennytm

Add functions to add unsigned and signed integers

This PR adds methods to unsigned integers to add signed integers with good overflow semantics under `#![feature(mixed_integer_ops)]`.

The added API is:

```rust
// `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;
}
```

Maybe it would be interesting to also have `add_signed` that panics in debug and wraps in release ?
library/core/src/lib.rs
library/core/src/num/int_macros.rs
library/core/src/num/uint_macros.rs
library/std/src/lib.rs