Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
46b8495
Implement `unsigned_signed_diff`
davidzeng0 Jun 23, 2024
e13eb37
Fix malformed suggestion for repeated maybe unsized bounds
gurry Jul 14, 2024
b74f426
Fix some `#[cfg_attr(not(doc), repr(..))]`
GrigorenkoPV Jul 21, 2024
a4dd0d6
std: use duplicate thread local state in tests
joboet Jul 24, 2024
dfb3fb3
Improved clarity of documentation for std::fs::create_dir_all
rik86189 Jul 24, 2024
4d5ac84
Remove Unnecessary `.as_str()` Conversions
veera-sivarajan Jul 24, 2024
fdff100
Add regression test
oli-obk Mar 8, 2024
acba644
Do not try to reveal hidden types when trying to prove Freeze in the …
oli-obk Mar 8, 2024
548c447
Add regression tests
oli-obk Jun 4, 2024
8ea461d
Do not assemble candidates for auto traits of opaque types in their d…
oli-obk Jun 4, 2024
c9886a1
Mark `missing_fragment_specifier` as `FutureReleaseErrorReportInDeps`
tgross35 Jul 23, 2024
6bf5fd5
Rollup merge of #122192 - oli-obk:type_of_opaque_for_const_checks, r=…
matthiaskrgr Jul 24, 2024
7fac549
Rollup merge of #126042 - davidzeng0:master, r=Amanieu
matthiaskrgr Jul 24, 2024
ed5dfed
Rollup merge of #126548 - rik86189:issue-88264-fix, r=tgross35
matthiaskrgr Jul 24, 2024
2ff33bb
Rollup merge of #127717 - gurry:127441-stray-impl-sugg, r=compiler-er…
matthiaskrgr Jul 24, 2024
07947f3
Rollup merge of #128046 - GrigorenkoPV:90435, r=tgross35
matthiaskrgr Jul 24, 2024
dec0c48
Rollup merge of #128122 - tgross35:missing-fragment-specifier-uncondi…
matthiaskrgr Jul 24, 2024
d146ecd
Rollup merge of #128135 - joboet:reduplicate_tls, r=tgross35
matthiaskrgr Jul 24, 2024
104a421
Rollup merge of #128140 - veera-sivarajan:remove-ident-to-str-convers…
matthiaskrgr Jul 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement unsigned_signed_diff
  • Loading branch information
davidzeng0 committed Jun 23, 2024
commit 46b84956f8391b90a4c140a85270571954309bbf
61 changes: 61 additions & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,67 @@ macro_rules! uint_impl {
}
}

#[doc = concat!(
"Checked integer subtraction. Computes `self - rhs` and checks if the result fits into an [`",
stringify!($SignedT), "`], returning `None` if overflow occurred."
)]
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(unsigned_signed_diff)]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_signed_diff(2), Some(8));")]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_signed_diff(10), Some(-8));")]
#[doc = concat!(
"assert_eq!(",
stringify!($SelfT),
"::MAX.checked_signed_diff(",
stringify!($SignedT),
"::MAX as ",
stringify!($SelfT),
"), None);"
)]
#[doc = concat!(
"assert_eq!((",
stringify!($SignedT),
"::MAX as ",
stringify!($SelfT),
").checked_signed_diff(",
stringify!($SelfT),
"::MAX), Some(",
stringify!($SignedT),
"::MIN));"
)]
#[doc = concat!(
"assert_eq!((",
stringify!($SignedT),
"::MAX as ",
stringify!($SelfT),
" + 1).checked_signed_diff(0), None);"
)]
#[doc = concat!(
"assert_eq!(",
stringify!($SelfT),
"::MAX.checked_signed_diff(",
stringify!($SelfT),
"::MAX), Some(0));"
)]
/// ```
#[unstable(feature = "unsigned_signed_diff", issue = "126041")]
#[inline]
pub const fn checked_signed_diff(self, rhs: Self) -> Option<$SignedT> {
let res = self.wrapping_sub(rhs) as $SignedT;
let overflow = (self >= rhs) == (res < 0);

if !overflow {
Some(res)
} else {
None
}
}

/// Checked integer multiplication. Computes `self * rhs`, returning
/// `None` if overflow occurred.
///
Expand Down