From 960dae29b4667c8b3588342de576ae4e08c1797a Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 26 Aug 2019 15:07:27 -0400 Subject: [PATCH 1/3] Add unchecked math inherant impls to integers --- src/libcore/num/mod.rs | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index b46e06f8d8ada..408ccea17eae5 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -632,6 +632,18 @@ $EndFeature, " } } + doc_comment! { + concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow +cannot occur."), + #[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 +668,18 @@ $EndFeature, " } } + doc_comment! { + concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow +cannot occur."), + #[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 +2710,18 @@ assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);", } } + doc_comment! { + concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow +cannot occur."), + #[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 +2744,18 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, " } } + doc_comment! { + concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow +cannot occur."), + #[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. From 107ba4252a0cd7f4c95882ef88268e2dc664d4ec Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 26 Aug 2019 15:17:14 -0400 Subject: [PATCH 2/3] Expand unchecked_add|sub documentation --- src/libcore/num/mod.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 408ccea17eae5..10c0d3f97d6c5 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -634,7 +634,10 @@ $EndFeature, " doc_comment! { concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow -cannot occur."), +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"] @@ -670,7 +673,10 @@ $EndFeature, " doc_comment! { concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow -cannot occur."), +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"] @@ -2712,7 +2718,10 @@ assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);", doc_comment! { concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow -cannot occur."), +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"] @@ -2746,7 +2755,10 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, " doc_comment! { concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow -cannot occur."), +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"] From 29a3454934c5b074e46f587d6d754f5987371db9 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 26 Aug 2019 15:39:19 -0400 Subject: [PATCH 3/3] Fix missing " --- src/libcore/num/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 10c0d3f97d6c5..7b5728e396a46 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -637,7 +637,7 @@ $EndFeature, " cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT), "::max_value()` -or `self + rhs < ", stringify!($SelfT), "::min_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"]