Skip to content

Commit

Permalink
Merge rust-bitcoin#3627: Mark functions const in fee_rate
Browse files Browse the repository at this point in the history
68aac98 Mark functions `const` (Jamil Lambert, PhD)

Pull request description:

  Following on from rust-bitcoin#3608 and rust-bitcoin#1174 more functions have been marked as const.

  Mark `checked_` functions in `units/src/fee_rate.rs` as const.
  Replace `map()` and `?` operators, which are not allowed in const context, with match statements.

ACKs for top commit:
  apoelstra:
    ACK 68aac98; successfully ran local tests; nice
  tcharding:
    ACK 68aac98

Tree-SHA512: 2165f0fdb2bbbd68223646c7f788ec3b637cf2bec06a9faee8b252900fac0517985047d5e88ec074b6d9121fcb3a3774a62f326e53e1ceaa041d65a4e2e35fb6
  • Loading branch information
apoelstra committed Nov 16, 2024
2 parents 055a462 + 68aac98 commit a3e9bad
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions units/src/fee_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,24 @@ impl FeeRate {
/// Checked multiplication.
///
/// Computes `self * rhs` returning [`None`] if 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(res) => Some(Self(res)),
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(res) => Some(Self(res)),
None => None,
}
}

/// Checked weight multiplication.
///
Expand All @@ -94,20 +106,38 @@ impl FeeRate {
/// rounded down.
///
/// [`None`] is returned if an overflow occurred.
pub fn checked_mul_by_weight(self, rhs: Weight) -> Option<Amount> {
let sats = self.0.checked_mul(rhs.to_wu())?.checked_add(999)? / 1000;
Some(Amount::from_sat(sats))
pub const fn checked_mul_by_weight(self, rhs: Weight) -> Option<Amount> {
// No `?` operator in const context.
match self.0.checked_mul(rhs.to_wu()) {
Some(mul_res) => match mul_res.checked_add(999) {
Some(add_res) => Some(Amount::from_sat(add_res / 1000)),
None => None,
},
None => None,
}
}

/// Checked addition.
///
/// Computes `self + rhs` returning [`None`] if overflow occured.
pub fn checked_add(self, rhs: u64) -> Option<Self> { self.0.checked_add(rhs).map(Self) }
pub const fn checked_add(self, rhs: u64) -> Option<Self> {
// No `map()` in const context.
match self.0.checked_add(rhs) {
Some(res) => Some(Self(res)),
None => None,
}
}

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

/// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`]
/// if an overflow occurred.
Expand Down

0 comments on commit a3e9bad

Please sign in to comment.