-
Notifications
You must be signed in to change notification settings - Fork 526
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
173 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,3 +1,4 @@ | ||
pub mod overflowing; | ||
pub mod wrapping; | ||
pub mod checked; | ||
pub mod saturating; |
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,58 @@ | ||
/// Performs addition that saturates at the numeric bounds instead of overflowing. | ||
pub trait SaturatingAdd<T> { | ||
/// Saturating addition. Computes `self + other`, saturating at the relevant high or low | ||
/// boundary of the type. | ||
fn saturating_add(self: T, other: T) -> T; | ||
} | ||
|
||
/// Performs subtraction that saturates at the numeric bounds instead of overflowing. | ||
pub trait SaturatingSub<T> { | ||
/// Saturating subtraction. Computes `self - other`, saturating at the relevant high or low | ||
/// boundary of the type. | ||
fn saturating_sub(self: T, other: T) -> T; | ||
} | ||
|
||
/// Performs multiplication that saturates at the numeric bounds instead of overflowing. | ||
pub trait SaturatingMul<T> { | ||
/// Saturating multiplication. Computes `self * other`, saturating at the relevant high or low | ||
/// boundary of the type. | ||
fn saturating_mul(self: T, other: T) -> T; | ||
} | ||
|
||
pub(crate) mod overflow_based { | ||
pub(crate) impl TSaturatingAdd< | ||
T, +Drop<T>, +core::num::traits::OverflowingAdd<T>, +core::integer::BoundedInt<T> | ||
> of core::num::traits::SaturatingAdd<T> { | ||
fn saturating_add(self: T, other: T) -> T { | ||
let (result, overflow) = self.overflowing_add(other); | ||
match overflow { | ||
true => core::integer::BoundedInt::max(), | ||
false => result, | ||
} | ||
} | ||
} | ||
|
||
pub(crate) impl TSaturatingSub< | ||
T, +Drop<T>, +core::num::traits::OverflowingSub<T>, +core::integer::BoundedInt<T> | ||
> of core::num::traits::SaturatingSub<T> { | ||
fn saturating_sub(self: T, other: T) -> T { | ||
let (result, overflow) = self.overflowing_sub(other); | ||
match overflow { | ||
true => core::integer::BoundedInt::min(), | ||
false => result, | ||
} | ||
} | ||
} | ||
|
||
pub(crate) impl TSaturatingMul< | ||
T, +Drop<T>, +core::num::traits::OverflowingMul<T>, +core::integer::BoundedInt<T> | ||
> of core::num::traits::SaturatingMul<T> { | ||
fn saturating_mul(self: T, other: T) -> T { | ||
let (result, overflow) = self.overflowing_mul(other); | ||
match overflow { | ||
true => core::integer::BoundedInt::max(), | ||
false => 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