Skip to content

Commit

Permalink
add methods for explicit overflow control
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Mar 29, 2021
1 parent d350da2 commit 5ae4707
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/std/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,40 @@ impl Uint128 {
.ok_or_else(|| StdError::devide_by_zero(self))
}

pub fn checked_div_euclid(self, other: Self) -> StdResult<Self> {
self.0
.checked_div_euclid(other.0)
.map(Self)
.ok_or_else(|| StdError::devide_by_zero(self))
}

pub fn checked_rem(self, other: Self) -> StdResult<Self> {
self.0
.checked_rem(other.0)
.map(Self)
.ok_or_else(|| StdError::devide_by_zero(self))
}

pub fn checked_add(self, other: Self) -> Option<Self> {
self.0.checked_add(other.0).map(Self)
}

pub fn checked_sub(self, other: Self) -> Option<Self> {
self.0.checked_sub(other.0).map(Self)
}

pub fn checked_mul(self, other: Self) -> Option<Self> {
self.0.checked_mul(other.0).map(Self)
}

pub fn checked_div(self, other: Self) -> Option<Self> {
self.0.checked_div(other.0).map(Self)
}

pub fn checked_rem(self, other: Self) -> Option<Self> {
self.0.checked_rem(other.0).map(Self)
}

pub fn wrapping_add(self, other: Self) -> Self {
Self(self.0.wrapping_add(other.0))
}
Expand All @@ -228,14 +255,30 @@ impl Uint128 {
Self(self.0.wrapping_mul(other.0))
}

pub fn wrapping_neg(self) -> Self {
Self(self.0.wrapping_neg())
}

pub fn wrapping_div(self, other: Self) -> Self {
Self(self.0.wrapping_div(other.0))
}

pub fn wrapping_div_euclid(self, other: Self) -> Self {
Self(self.0.wrapping_div_euclid(other.0))
}

pub fn wrapping_rem(self, other: Self) -> Self {
Self(self.0.wrapping_rem(other.0))
}

pub fn wrapping_rem_euclid(self, other: Self) -> Self {
Self(self.0.wrapping_rem_euclid(other.0))
}

pub fn wrapping_pow(self, other: u32) -> Self {
Self(self.0.wrapping_pow(other))
}

pub fn saturating_add(self, other: Self) -> Self {
Self(self.0.saturating_add(other.0))
}
Expand All @@ -247,6 +290,10 @@ impl Uint128 {
pub fn saturating_mul(self, other: Self) -> Self {
Self(self.0.saturating_mul(other.0))
}

pub fn saturating_pow(self, other: u32) -> Self {
Self(self.0.saturating_pow(other))
}
}

// `From<u{128,64,32,16,8}>` is implemented manually instead of
Expand Down

0 comments on commit 5ae4707

Please sign in to comment.