Merge rust-bitcoin/rust-bitcoin#3636: Mark `checked_` functions as `const`

a8379bf005 Mark `checked_` functions const in bitcoin. (Jamil Lambert, PhD)
fe10ff2eb7 Mark functions `const` in `units` (Jamil Lambert, PhD)

Pull request description:

  Following on #3608, #3627 and #1174 the rest of the `checked_` functions in all `rust-bitcoin` have been marked as const.  Except for tests and traits.

ACKs for top commit:
  apoelstra:
    ACK a8379bf0053e66cf5984ce449d19e54e529b6b70; successfully ran local tests; thanks! I find this much easier to read
  tcharding:
    ACK a8379bf005

Tree-SHA512: b8d97b7a3d9fc33b57349f418ccc5aac2f3e8c4145a73bf0e24e85547217d41214696e9a80b1fb5a1bae7e766aad1c2b5df6044564772fe76cec9b3628bcd8e5
This commit is contained in:
merge-script 2024-11-23 11:46:29 +00:00
commit e0ba1b661c
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 96 additions and 28 deletions

View File

@ -627,8 +627,11 @@ impl Psbt {
/// Gets the input at `input_index` after checking that it is a valid index.
fn checked_input(&self, input_index: usize) -> Result<&Input, IndexOutOfBoundsError> {
self.check_index_is_within_bounds(input_index)?;
Ok(&self.inputs[input_index])
// No `?` operator in const context.
match self.check_index_is_within_bounds(input_index) {
Ok(_) => Ok(&self.inputs[input_index]),
Err(e) => Err(e),
}
}
/// Checks `input_index` is within bounds for the PSBT `inputs` array and

View File

@ -217,27 +217,45 @@ impl SignedAmount {
/// Consider using `unsigned_abs` which is often more practical.
///
/// Returns [`None`] if overflow occurred. (`self == MIN`)
pub fn checked_abs(self) -> Option<SignedAmount> { self.0.checked_abs().map(SignedAmount) }
pub const fn checked_abs(self) -> Option<SignedAmount> {
// No `map()` in const context.
match self.0.checked_abs() {
Some(res) => Some(SignedAmount(res)),
None => None,
}
}
/// Checked addition.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_add(self, rhs: SignedAmount) -> Option<SignedAmount> {
self.0.checked_add(rhs.0).map(SignedAmount)
pub const fn checked_add(self, rhs: SignedAmount) -> Option<SignedAmount> {
// No `map()` in const context.
match self.0.checked_add(rhs.0) {
Some(res) => Some(SignedAmount(res)),
None => None,
}
}
/// Checked subtraction.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_sub(self, rhs: SignedAmount) -> Option<SignedAmount> {
self.0.checked_sub(rhs.0).map(SignedAmount)
pub const fn checked_sub(self, rhs: SignedAmount) -> Option<SignedAmount> {
// No `map()` in const context.
match self.0.checked_sub(rhs.0) {
Some(res) => Some(SignedAmount(res)),
None => None,
}
}
/// Checked multiplication.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_mul(self, rhs: i64) -> Option<SignedAmount> {
self.0.checked_mul(rhs).map(SignedAmount)
pub const fn checked_mul(self, rhs: i64) -> Option<SignedAmount> {
// No `map()` in const context.
match self.0.checked_mul(rhs) {
Some(res) => Some(SignedAmount(res)),
None => None,
}
}
/// Checked integer division.
@ -245,15 +263,23 @@ impl SignedAmount {
/// Be aware that integer division loses the remainder if no exact division can be made.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_div(self, rhs: i64) -> Option<SignedAmount> {
self.0.checked_div(rhs).map(SignedAmount)
pub const fn checked_div(self, rhs: i64) -> Option<SignedAmount> {
// No `map()` in const context.
match self.0.checked_div(rhs) {
Some(res) => Some(SignedAmount(res)),
None => None,
}
}
/// Checked remainder.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_rem(self, rhs: i64) -> Option<SignedAmount> {
self.0.checked_rem(rhs).map(SignedAmount)
pub const fn checked_rem(self, rhs: i64) -> Option<SignedAmount> {
// No `map()` in const context.
match self.0.checked_rem(rhs) {
Some(res) => Some(SignedAmount(res)),
None => None,
}
}
/// Unchecked addition.

View File

@ -196,28 +196,48 @@ impl Amount {
/// Checked addition.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_add(self, rhs: Amount) -> Option<Amount> {
self.0.checked_add(rhs.0).map(Amount)
pub const fn checked_add(self, rhs: Amount) -> Option<Amount> {
// No `map()` in const context.
match self.0.checked_add(rhs.0) {
Some(res) => Some(Amount(res)),
None => None,
}
}
/// Checked subtraction.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_sub(self, rhs: Amount) -> Option<Amount> {
self.0.checked_sub(rhs.0).map(Amount)
pub const fn checked_sub(self, rhs: Amount) -> Option<Amount> {
// No `map()` in const context.
match self.0.checked_sub(rhs.0) {
Some(res) => Some(Amount(res)),
None => None,
}
}
/// Checked multiplication.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_mul(self, rhs: u64) -> Option<Amount> { self.0.checked_mul(rhs).map(Amount) }
pub const fn checked_mul(self, rhs: u64) -> Option<Amount> {
// No `map()` in const context.
match self.0.checked_mul(rhs) {
Some(res) => Some(Amount(res)),
None => None,
}
}
/// Checked integer division.
///
/// Be aware that integer division loses the remainder if no exact division can be made.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_div(self, rhs: u64) -> Option<Amount> { self.0.checked_div(rhs).map(Amount) }
pub const fn checked_div(self, rhs: u64) -> Option<Amount> {
// No `map()` in const context.
match self.0.checked_div(rhs) {
Some(res) => Some(Amount(res)),
None => None,
}
}
/// Checked weight ceiling division.
///
@ -227,12 +247,19 @@ impl Amount {
///
/// Returns [`None`] if overflow occurred.
#[cfg(feature = "alloc")]
pub fn checked_div_by_weight_ceil(self, rhs: Weight) -> Option<FeeRate> {
let sats = self.0.checked_mul(1_000)?;
pub const fn checked_div_by_weight_ceil(self, rhs: Weight) -> Option<FeeRate> {
let wu = rhs.to_wu();
let fee_rate = sats.checked_add(wu.checked_sub(1)?)?.checked_div(wu)?;
Some(FeeRate::from_sat_per_kwu(fee_rate))
// No `?` operator in const context.
if let Some(sats) = self.0.checked_mul(1_000) {
if let Some(wu_minus_one) = wu.checked_sub(1) {
if let Some(sats_plus_wu_minus_one) = sats.checked_add(wu_minus_one) {
if let Some(fee_rate) = sats_plus_wu_minus_one.checked_div(wu) {
return Some(FeeRate::from_sat_per_kwu(fee_rate));
}
}
}
}
None
}
/// Checked weight floor division.
@ -242,15 +269,27 @@ impl Amount {
///
/// Returns [`None`] if overflow occurred.
#[cfg(feature = "alloc")]
pub fn checked_div_by_weight_floor(self, rhs: Weight) -> Option<FeeRate> {
let fee_rate = self.0.checked_mul(1_000)?.checked_div(rhs.to_wu())?;
Some(FeeRate::from_sat_per_kwu(fee_rate))
pub const fn checked_div_by_weight_floor(self, rhs: Weight) -> Option<FeeRate> {
// No `?` operator in const context.
match self.0.checked_mul(1_000) {
Some(res) => match res.checked_div(rhs.to_wu()) {
Some(fee_rate) => Some(FeeRate::from_sat_per_kwu(fee_rate)),
None => None,
},
None => None,
}
}
/// Checked remainder.
///
/// Returns [`None`] if overflow occurred.
pub fn checked_rem(self, rhs: u64) -> Option<Amount> { self.0.checked_rem(rhs).map(Amount) }
pub const fn checked_rem(self, rhs: u64) -> Option<Amount> {
// No `map()` in const context.
match self.0.checked_rem(rhs) {
Some(res) => Some(Amount(res)),
None => None,
}
}
/// Unchecked addition.
///