Add inherent impls for unchecked math intrinsics

This commit is contained in:
CAD97 2020-02-03 14:54:02 -05:00
parent 42abbd8878
commit b70e7fd0db

View file

@ -697,6 +697,23 @@ $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",
reason = "niche optimization path",
issue = "none",
)]
#[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.
@ -722,6 +739,23 @@ $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",
reason = "niche optimization path",
issue = "none",
)]
#[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.
@ -747,6 +781,23 @@ $EndFeature, "
}
}
doc_comment! {
concat!("Unchecked integer multiplication. 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",
reason = "niche optimization path",
issue = "none",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
intrinsics::unchecked_mul(self, rhs)
}
}
doc_comment! {
concat!("Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
or the division results in overflow.
@ -2884,6 +2935,23 @@ assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", $EndFeat
}
}
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",
reason = "niche optimization path",
issue = "none",
)]
#[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.
@ -2907,6 +2975,23 @@ 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",
reason = "niche optimization path",
issue = "none",
)]
#[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.
@ -2930,6 +3015,23 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", $EndFeature, "
}
}
doc_comment! {
concat!("Unchecked integer multiplication. 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",
reason = "niche optimization path",
issue = "none",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
intrinsics::unchecked_mul(self, rhs)
}
}
doc_comment! {
concat!("Checked integer division. Computes `self / rhs`, returning `None`
if `rhs == 0`.