Skip to content

Commit

Permalink
Merge rust-bitcoin#3608: Mark funtions const
Browse files Browse the repository at this point in the history
4c94695 Mark funtions const (yancy)

Pull request description:

  May as well mark these const as well.  Follow up to rust-bitcoin#3605

ACKs for top commit:
  apoelstra:
    ACK 4c94695; successfully ran local tests; yeah, I think this are all worth consting
  tcharding:
    ACK 4c94695
  sanket1729:
    ACK 4c94695

Tree-SHA512: bf41e416cb8a2eb90e26d7bd640df5c2a5a868e3b721bde7cc8a3b97c687bab4d1cb4ab1c4b04ed625e8657e25c59e386720cb0f9d735ffc492e98f5b8b2ca3c
  • Loading branch information
apoelstra committed Nov 13, 2024
2 parents 3870121 + 4c94695 commit 7315ca9
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions units/src/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,46 @@ impl Weight {
/// Checked addition.
///
/// Computes `self + rhs` returning [`None`] if an overflow occurred.
pub fn checked_add(self, rhs: Self) -> Option<Self> { self.0.checked_add(rhs.0).map(Self) }
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
// No `map()` in const context.
match self.0.checked_add(rhs.0) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None
}
}

/// Checked subtraction.
///
/// Computes `self - rhs` returning [`None`] if an overflow occurred.
pub fn checked_sub(self, rhs: Self) -> Option<Self> { self.0.checked_sub(rhs.0).map(Self) }
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
// No `map()` in const context.
match self.0.checked_sub(rhs.0) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None
}
}

/// Checked multiplication.
///
/// Computes `self * rhs` returning [`None`] if an overflow occurred.
pub fn checked_mul(self, rhs: u64) -> Option<Self> { self.0.checked_mul(rhs).map(Self) }
pub const fn checked_mul(self, rhs: u64) -> Option<Self> {
// No `map()` in const context.
match self.0.checked_mul(rhs) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None
}
}

/// Checked division.
///
/// Computes `self / rhs` returning [`None`] if `rhs == 0`.
pub fn checked_div(self, rhs: u64) -> Option<Self> { self.0.checked_div(rhs).map(Self) }
pub const fn checked_div(self, rhs: u64) -> Option<Self> {
// No `map()` in const context.
match self.0.checked_div(rhs) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None
}
}
}

/// Alternative will display the unit.
Expand Down

0 comments on commit 7315ca9

Please sign in to comment.