Checked ops.
This commit is contained in:
parent
9e23e8f4a3
commit
ea29612eaa
54
src/uint.rs
54
src/uint.rs
|
@ -815,6 +815,14 @@ macro_rules! construct_uint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checked addition. Returns `None` if overflow occurred.
|
||||||
|
pub fn checked_add(self, other: $name) -> Option<$name> {
|
||||||
|
match self.overflowing_add(other) {
|
||||||
|
(_, true) => None,
|
||||||
|
(val, _) => Some(val),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Subtraction which underflows and returns a flag if it does.
|
/// Subtraction which underflows and returns a flag if it does.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn overflowing_sub(self, other: $name) -> ($name, bool) {
|
pub fn overflowing_sub(self, other: $name) -> ($name, bool) {
|
||||||
|
@ -829,6 +837,14 @@ macro_rules! construct_uint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checked subtraction. Returns `None` if overflow occurred.
|
||||||
|
pub fn checked_sub(self, other: $name) -> Option<$name> {
|
||||||
|
match self.overflowing_sub(other) {
|
||||||
|
(_, true) => None,
|
||||||
|
(val, _) => Some(val),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Multiply with overflow, returning a flag if it does.
|
/// Multiply with overflow, returning a flag if it does.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn overflowing_mul(self, other: $name) -> ($name, bool) {
|
pub fn overflowing_mul(self, other: $name) -> ($name, bool) {
|
||||||
|
@ -843,20 +859,58 @@ macro_rules! construct_uint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checked multiplication. Returns `None` if overflow occurred.
|
||||||
|
pub fn checked_mul(self, other: $name) -> Option<$name> {
|
||||||
|
match self.overflowing_mul(other) {
|
||||||
|
(_, true) => None,
|
||||||
|
(val, _) => Some(val),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Division with overflow
|
/// Division with overflow
|
||||||
pub fn overflowing_div(self, other: $name) -> ($name, bool) {
|
pub fn overflowing_div(self, other: $name) -> ($name, bool) {
|
||||||
(self / other, false)
|
(self / other, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checked division. Returns `None` if `other == 0`.
|
||||||
|
pub fn checked_div(self, other: $name) -> Option<$name> {
|
||||||
|
if other.is_zero() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(self / other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Modulus with overflow.
|
/// Modulus with overflow.
|
||||||
pub fn overflowing_rem(self, other: $name) -> ($name, bool) {
|
pub fn overflowing_rem(self, other: $name) -> ($name, bool) {
|
||||||
(self % other, false)
|
(self % other, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checked modulus. Returns `None` if `other == 0`.
|
||||||
|
pub fn checked_rem(self, other: $name) -> Option<$name> {
|
||||||
|
if other.is_zero() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(self % other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Negation with overflow.
|
/// Negation with overflow.
|
||||||
pub fn overflowing_neg(self) -> ($name, bool) {
|
pub fn overflowing_neg(self) -> ($name, bool) {
|
||||||
|
if self.is_zero() {
|
||||||
|
(self, false)
|
||||||
|
} else {
|
||||||
(!self, true)
|
(!self, true)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checked negation. Returns `None` unless `self == 0`.
|
||||||
|
pub fn checked_neg(self) -> Option<$name> {
|
||||||
|
match self.overflowing_neg() {
|
||||||
|
(_, true) => None,
|
||||||
|
(zero, false) => Some(zero),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Multiplication by u32
|
/// Multiplication by u32
|
||||||
pub fn mul_u32(self, other: u32) -> Self {
|
pub fn mul_u32(self, other: u32) -> Self {
|
||||||
|
|
Loading…
Reference in New Issue