Merge rust-bitcoin/rust-bitcoin#4539: units: re-order ceil/floor functions

1fef5a3dc5 units: re-order ceil/floor functions (Tobin C. Harding)

Pull request description:

  We have 4 functions, 2 ceil, 2 floor but they are ordered ceil, floor, floor, ceil - this causes my head to twitch when I read it.

  The logic in the floor versions is easier so put them first, this is uniform with `fee_rate/mod.rs`.

  Code move only.

ACKs for top commit:
  apoelstra:
    ACK 1fef5a3dc55bfc1858c32f81f18840ec1d01c807; successfully ran local tests; will one-ack merge on the basis that this is trivial

Tree-SHA512: 8f15a34ee637cb6aa4013385e1e7f7c70c2e760908bec01d43a96b9c85a3d309c7b9528f13ec5f072f1166511eb7560d1b5aefdccc1a44922c92e68d527fe1a5
This commit is contained in:
merge-script 2025-05-22 23:39:23 +00:00
commit d75aba2061
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 18 additions and 18 deletions

View File

@ -18,6 +18,24 @@ use NumOpResult as R;
use crate::{Amount, FeeRate, MathOp, NumOpError as E, NumOpResult, OptionExt, Weight};
impl Amount {
/// Checked weight floor division.
///
/// Be aware that integer division loses the remainder if no exact division
/// can be made. See also [`Self::checked_div_by_weight_ceil`].
///
/// Returns [`None`] if overflow occurred.
#[must_use]
pub const fn checked_div_by_weight_floor(self, weight: Weight) -> Option<FeeRate> {
// No `?` operator in const context.
match self.to_sat().checked_mul(1_000) {
Some(res) => match res.checked_div(weight.to_wu()) {
Some(fee_rate) => Some(FeeRate::from_sat_per_kwu(fee_rate)),
None => None,
},
None => None,
}
}
/// Checked weight ceiling division.
///
/// Be aware that integer division loses the remainder if no exact division
@ -52,24 +70,6 @@ impl Amount {
None
}
/// Checked weight floor division.
///
/// Be aware that integer division loses the remainder if no exact division
/// can be made. See also [`Self::checked_div_by_weight_ceil`].
///
/// Returns [`None`] if overflow occurred.
#[must_use]
pub const fn checked_div_by_weight_floor(self, weight: Weight) -> Option<FeeRate> {
// No `?` operator in const context.
match self.to_sat().checked_mul(1_000) {
Some(res) => match res.checked_div(weight.to_wu()) {
Some(fee_rate) => Some(FeeRate::from_sat_per_kwu(fee_rate)),
None => None,
},
None => None,
}
}
/// Checked fee rate floor division.
///
/// Computes the maximum weight that would result in a fee less than or equal to this amount