-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
263 additions
and
1 deletion.
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
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
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,2 +1,3 @@ | ||
pub mod overflowing; | ||
pub mod wrapping; | ||
pub mod checked; |
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,60 @@ | ||
/// Performs addition that returns `None` instead of wrapping around on | ||
/// overflow. | ||
pub trait CheckedAdd<T> { | ||
/// Adds two numbers, checking for overflow. If overflow happens, `None` is | ||
/// returned. | ||
fn checked_add(self: T, v: T) -> Option<T>; | ||
} | ||
|
||
/// Performs subtraction that returns `None` instead of wrapping around on underflow. | ||
pub trait CheckedSub<T> { | ||
/// Subtracts two numbers, checking for underflow. If underflow happens, | ||
/// `None` is returned. | ||
fn checked_sub(self: T, v: T) -> Option<T>; | ||
} | ||
|
||
/// Performs multiplication that returns `None` instead of wrapping around on underflow or | ||
/// overflow. | ||
pub trait CheckedMul<T> { | ||
/// Multiplies two numbers, checking for underflow or overflow. If underflow | ||
/// or overflow happens, `None` is returned. | ||
fn checked_mul(self: T, v: T) -> Option<T>; | ||
} | ||
|
||
pub(crate) mod overflow_based { | ||
pub(crate) impl TCheckedAdd< | ||
T, +Drop<T>, +core::num::traits::OverflowingAdd<T> | ||
> of core::num::traits::CheckedAdd<T> { | ||
fn checked_add(self: T, v: T) -> Option<T> { | ||
let (result, overflow) = self.overflowing_add(v); | ||
match overflow { | ||
true => Option::None, | ||
false => Option::Some(result), | ||
} | ||
} | ||
} | ||
|
||
pub(crate) impl TCheckedSub< | ||
T, +Drop<T>, +core::num::traits::OverflowingSub<T> | ||
> of core::num::traits::CheckedSub<T> { | ||
fn checked_sub(self: T, v: T) -> Option<T> { | ||
let (result, overflow) = self.overflowing_sub(v); | ||
match overflow { | ||
true => Option::None, | ||
false => Option::Some(result), | ||
} | ||
} | ||
} | ||
|
||
pub(crate) impl TCheckedMul< | ||
T, +Drop<T>, +core::num::traits::OverflowingMul<T> | ||
> of core::num::traits::CheckedMul<T> { | ||
fn checked_mul(self: T, v: T) -> Option<T> { | ||
let (result, overflow) = self.overflowing_mul(v); | ||
match overflow { | ||
true => Option::None, | ||
false => Option::Some(result), | ||
} | ||
} | ||
} | ||
} |
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