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 2f209f6
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/std/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ 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)
Expand All @@ -228,14 +235,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 +270,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 2f209f6

Please sign in to comment.