Merge rust-bitcoin/rust-bitcoin#3627: Mark functions `const` in `fee_rate`

68aac98753 Mark functions `const` (Jamil Lambert, PhD)

Pull request description:

  Following on from #3608 and #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 68aac98753dba77bccc6572c65d441108dc53a17; successfully ran local tests; nice
  tcharding:
    ACK 68aac98753

Tree-SHA512: 2165f0fdb2bbbd68223646c7f788ec3b637cf2bec06a9faee8b252900fac0517985047d5e88ec074b6d9121fcb3a3774a62f326e53e1ceaa041d65a4e2e35fb6
This commit is contained in:
merge-script 2024-11-16 02:25:25 +00:00
commit a3e9bad990
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 37 additions and 7 deletions

View File

@ -79,12 +79,24 @@ impl FeeRate {
/// Checked multiplication. /// Checked multiplication.
/// ///
/// Computes `self * rhs` returning [`None`] if overflow occurred. /// 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. /// Checked division.
/// ///
/// Computes `self / rhs` returning [`None`] if `rhs == 0`. /// 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. /// Checked weight multiplication.
/// ///
@ -94,20 +106,38 @@ impl FeeRate {
/// rounded down. /// rounded down.
/// ///
/// [`None`] is returned if an overflow occurred. /// [`None`] is returned if an overflow occurred.
pub fn checked_mul_by_weight(self, rhs: Weight) -> Option<Amount> { pub const fn checked_mul_by_weight(self, rhs: Weight) -> Option<Amount> {
let sats = self.0.checked_mul(rhs.to_wu())?.checked_add(999)? / 1000; // No `?` operator in const context.
Some(Amount::from_sat(sats)) 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. /// Checked addition.
/// ///
/// Computes `self + rhs` returning [`None`] if overflow occured. /// 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. /// Checked subtraction.
/// ///
/// Computes `self - rhs` returning [`None`] if overflow occured. /// 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`] /// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`]
/// if an overflow occurred. /// if an overflow occurred.