diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index b46e06f8d8ada..7b5728e396a46 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -632,6 +632,21 @@ $EndFeature, " } } + doc_comment! { + concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow +cannot occur. + +This results in undefined behavior when `self + rhs > ", stringify!($SelfT), "::max_value()` +or `self + rhs < ", stringify!($SelfT), "::min_value()`."), + #[unstable(feature = "unchecked_math", issue = "0")] + #[must_use = "this returns the result of the operation, \ + without modifying the original"] + #[inline] + pub unsafe fn unchecked_add(self, rhs: Self) -> Self { + intrinsics::unchecked_add(self, rhs) + } + } + doc_comment! { concat!("Checked integer subtraction. Computes `self - rhs`, returning `None` if overflow occurred. @@ -656,6 +671,21 @@ $EndFeature, " } } + doc_comment! { + concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow +cannot occur. + +This results in undefined behavior when `self - rhs > ", stringify!($SelfT), "::max_value()` +or `self - rhs < ", stringify!($SelfT), "::min_value()`."), + #[unstable(feature = "unchecked_math", issue = "0")] + #[must_use = "this returns the result of the operation, \ + without modifying the original"] + #[inline] + pub unsafe fn unchecked_sub(self, rhs: Self) -> Self { + intrinsics::unchecked_sub(self, rhs) + } + } + doc_comment! { concat!("Checked integer multiplication. Computes `self * rhs`, returning `None` if overflow occurred. @@ -2686,6 +2716,21 @@ assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);", } } + doc_comment! { + concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow +cannot occur. + +This results in undefined behavior when `self + rhs > ", stringify!($SelfT), "::max_value()` +or `self + rhs < ", stringify!($SelfT), "::min_value()`."), + #[unstable(feature = "unchecked_math", issue = "0")] + #[must_use = "this returns the result of the operation, \ + without modifying the original"] + #[inline] + pub unsafe fn unchecked_add(self, rhs: Self) -> Self { + intrinsics::unchecked_add(self, rhs) + } + } + doc_comment! { concat!("Checked integer subtraction. Computes `self - rhs`, returning `None` if overflow occurred. @@ -2708,6 +2753,21 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, " } } + doc_comment! { + concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow +cannot occur. + +This results in undefined behavior when `self - rhs > ", stringify!($SelfT), "::max_value()` +or `self - rhs < ", stringify!($SelfT), "::min_value()`."), + #[unstable(feature = "unchecked_math", issue = "0")] + #[must_use = "this returns the result of the operation, \ + without modifying the original"] + #[inline] + pub unsafe fn unchecked_sub(self, rhs: Self) -> Self { + intrinsics::unchecked_sub(self, rhs) + } + } + doc_comment! { concat!("Checked integer multiplication. Computes `self * rhs`, returning `None` if overflow occurred.