8000 Redesign the std::iter::Step trait by CAD97 · Pull Request #68807 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Redesign the std::iter::Step trait #68807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
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
Add inherent impls for unchecked math intrinsics
  • Loading branch information
CAD97 committed Feb 3, 2020
commit 39e3eedde9cea5b1e31cb7dae09dd1f66f73a706
102 changes: 102 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,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.
Expand All @@ -734,6 +751,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.
Expand All @@ -758,6 +792,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.
Expand Down Expand Up @@ -2856,6 +2907,23 @@ 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",
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.
Expand All @@ -2878,6 +2946,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.
Expand All @@ -2900,6 +2985,23 @@ assert_eq!(", stringify!($SelfT), "::max_value().checked_mul(2), None);", $EndFe
}
}

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 { 50B7
intrinsics::unchecked_mul(self, rhs)
}
}

doc_comment! {
concat!("Checked integer division. Computes `self / rhs`, returning `None`
if `rhs == 0`.
Expand Down
0