-
Notifications
You must be signed in to change notification settings - Fork 141
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
Add Overflowing trait #180
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod checked; | ||
pub mod inv; | ||
pub mod mul_add; | ||
pub mod overflowing; | ||
pub mod saturating; | ||
pub mod wrapping; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use core::ops::{Add, Mul, Sub}; | ||
#[cfg(has_i128)] | ||
use core::{i128, u128}; | ||
use core::{i16, i32, i64, i8, isize}; | ||
use core::{u16, u32, u64, u8, usize}; | ||
|
||
macro_rules! overflowing_impl { | ||
($trait_name:ident, $method:ident, $t:ty) => { | ||
impl $trait_name for $t { | ||
#[inline] | ||
fn $method(&self, v: &Self) -> (Self, bool) { | ||
<$t>::$method(*self, *v) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// If an overflow would have occurred then the wrapped value is returned. | ||
pub trait OverflowingAdd: Sized + Add<Self, Output = Self> { | ||
/// Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur. | ||
fn overflowing_add(&self, v: &Self) -> (Self, bool); | ||
} | ||
|
||
overflowing_impl!(OverflowingAdd, overflowing_add, u8); | ||
overflowing_impl!(OverflowingAdd, overflowing_add, u16); | ||
overflowing_impl!(OverflowingAdd, overflowing_add, u32); | ||
overflowing_impl!(OverflowingAdd, overflowing_add, u64); | ||
overflowing_impl!(OverflowingAdd, overflowing_add, usize); | ||
#[cfg(has_i128)] | ||
overflowing_impl!(OverflowingAdd, overflowing_add, u128); | ||
|
||
overflowing_impl!(OverflowingAdd, overflowing_add, i8); | ||
overflowing_impl!(OverflowingAdd, overflowing_add, i16); | ||
overflowing_impl!(OverflowingAdd, overflowing_add, i32); | ||
overflowing_impl!(OverflowingAdd, overflowing_add, i64); | ||
overflowing_impl!(OverflowingAdd, overflowing_add, isize); | ||
#[cfg(has_i128)] | ||
overflowing_impl!(OverflowingAdd, overflowing_add, i128); | ||
|
||
/// If an overflow would have occurred then the wrapped value is returned. | ||
pub trait OverflowingSub: Sized + Sub<Self, Output = Self> { | ||
/// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. | ||
fn overflowing_sub(&self, v: &Self) -> (Self, bool); | ||
} | ||
|
||
overflowing_impl!(OverflowingSub, overflowing_sub, u8); | ||
overflowing_impl!(OverflowingSub, overflowing_sub, u16); | ||
overflowing_impl!(OverflowingSub, overflowing_sub, u32); | ||
overflowing_impl!(OverflowingSub, overflowing_sub, u64); | ||
overflowing_impl!(OverflowingSub, overflowing_sub, usize); | ||
#[cfg(has_i128)] | ||
overflowing_impl!(OverflowingSub, overflowing_sub, u128); | ||
|
||
overflowing_impl!(OverflowingSub, overflowing_sub, i8); | ||
overflowing_impl!(OverflowingSub, overflowing_sub, i16); | ||
overflowing_impl!(OverflowingSub, overflowing_sub, i32); | ||
overflowing_impl!(OverflowingSub, overflowing_sub, i64); | ||
overflowing_impl!(OverflowingSub, overflowing_sub, isize); | ||
#[cfg(has_i128)] | ||
overflowing_impl!(OverflowingSub, overflowing_sub, i128); | ||
|
||
/// If an overflow would have occurred then the wrapped value is returned. | ||
pub trait OverflowingMul: Sized + Mul<Self, Output = Self> { | ||
/// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. | ||
fn overflowing_mul(&self, v: &Self) -> (Self, bool); | ||
} | ||
|
||
overflowing_impl!(OverflowingMul, overflowing_mul, u8); | ||
overflowing_impl!(OverflowingMul, overflowing_mul, u16); | ||
overflowing_impl!(OverflowingMul, overflowing_mul, u32); | ||
overflowing_impl!(OverflowingMul, overflowing_mul, u64); | ||
overflowing_impl!(OverflowingMul, overflowing_mul, usize); | ||
#[cfg(has_i128)] | ||
overflowing_impl!(OverflowingMul, overflowing_mul, u128); | ||
|
||
overflowing_impl!(OverflowingMul, overflowing_mul, i8); | ||
overflowing_impl!(OverflowingMul, overflowing_mul, i16); | ||
overflowing_impl!(OverflowingMul, overflowing_mul, i32); | ||
overflowing_impl!(OverflowingMul, overflowing_mul, i64); | ||
overflowing_impl!(OverflowingMul, overflowing_mul, isize); | ||
#[cfg(has_i128)] | ||
overflowing_impl!(OverflowingMul, overflowing_mul, i128); | ||
|
||
#[test] | ||
fn test_overflowing_traits() { | ||
fn overflowing_add<T: OverflowingAdd>(a: T, b: T) -> (T, bool) { | ||
a.overflowing_add(&b) | ||
} | ||
fn overflowing_sub<T: OverflowingSub>(a: T, b: T) -> (T, bool) { | ||
a.overflowing_sub(&b) | ||
} | ||
fn overflowing_mul<T: OverflowingMul>(a: T, b: T) -> (T, bool) { | ||
a.overflowing_mul(&b) | ||
} | ||
assert_eq!(overflowing_add(5i16, 2), (7, false)); | ||
assert_eq!(overflowing_add(i16::MAX, 1), (i16::MIN, true)); | ||
assert_eq!(overflowing_sub(5i16, 2), (3, false)); | ||
assert_eq!(overflowing_sub(i16::MIN, 1), (i16::MAX, true)); | ||
assert_eq!(overflowing_mul(5i16, 2), (10, false)); | ||
assert_eq!(overflowing_mul(1_000_000_000i32, 10), (1410065408, true)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line seems like it would better belong as the second sentence of the method docs. At the top we need a more general description, perhaps something simple like "Performs addition with a flag for overflow."
The same goes for OverflowingSub/Mul.