Merge rust-bitcoin/rust-bitcoin#4604: units: Access with getters instead of inner field

b843f1356d units: Access with getters instead of inner field (Tobin C. Harding)

Pull request description:

  The `block` module does not follow the `encapsulate` pattern but we can still use the getters instead of accessing the inner field directly.

  Refactor, no logic change.

ACKs for top commit:
  apoelstra:
    ACK b843f1356ddfb2f23e6715708f99d40b82dce17a; successfully ran local tests

Tree-SHA512: 79bc1a8722987ca08d2ce33737a9b24998d7ee5d54b3fb065b906b64e33565eca5d5c35836cc029355c47037945cf43f6151374a8cb3ddd222991345aa41819f
This commit is contained in:
merge-script 2025-06-10 15:18:37 +00:00
commit b7b9d42b4a
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 20 additions and 12 deletions

View File

@ -90,13 +90,13 @@ impl BlockHeight {
/// Attempt to subtract two [`BlockHeight`]s, returning `None` in case of overflow.
#[must_use]
pub fn checked_sub(self, other: Self) -> Option<BlockHeightInterval> {
self.0.checked_sub(other.0).map(BlockHeightInterval)
self.to_u32().checked_sub(other.to_u32()).map(BlockHeightInterval)
}
/// Attempt to add an interval to this [`BlockHeight`], returning `None` in case of overflow.
#[must_use]
pub fn checked_add(self, other: BlockHeightInterval) -> Option<Self> {
self.0.checked_add(other.0).map(Self)
self.to_u32().checked_add(other.to_u32()).map(Self)
}
}
@ -150,11 +150,15 @@ impl BlockHeightInterval {
/// Attempt to subtract two [`BlockHeightInterval`]s, returning `None` in case of overflow.
#[must_use]
pub fn checked_sub(self, other: Self) -> Option<Self> { self.0.checked_sub(other.0).map(Self) }
pub fn checked_sub(self, other: Self) -> Option<Self> {
self.to_u32().checked_sub(other.to_u32()).map(Self)
}
/// Attempt to add two [`BlockHeightInterval`]s, returning `None` in case of overflow.
#[must_use]
pub fn checked_add(self, other: Self) -> Option<Self> { self.0.checked_add(other.0).map(Self) }
pub fn checked_add(self, other: Self) -> Option<Self> {
self.to_u32().checked_add(other.to_u32()).map(Self)
}
}
impl From<relative::NumberOfBlocks> for BlockHeightInterval {
@ -223,13 +227,13 @@ impl BlockMtp {
/// Attempt to subtract two [`BlockMtp`]s, returning `None` in case of overflow.
#[must_use]
pub fn checked_sub(self, other: Self) -> Option<BlockMtpInterval> {
self.0.checked_sub(other.0).map(BlockMtpInterval)
self.to_u32().checked_sub(other.to_u32()).map(BlockMtpInterval)
}
/// Attempt to add an interval to this [`BlockMtp`], returning `None` in case of overflow.
#[must_use]
pub fn checked_add(self, other: BlockMtpInterval) -> Option<Self> {
self.0.checked_add(other.0).map(Self)
self.to_u32().checked_add(other.to_u32()).map(Self)
}
}
@ -315,11 +319,15 @@ impl BlockMtpInterval {
/// Attempt to subtract two [`BlockMtpInterval`]s, returning `None` in case of overflow.
#[must_use]
pub fn checked_sub(self, other: Self) -> Option<Self> { self.0.checked_sub(other.0).map(Self) }
pub fn checked_sub(self, other: Self) -> Option<Self> {
self.to_u32().checked_sub(other.to_u32()).map(Self)
}
/// Attempt to add two [`BlockMtpInterval`]s, returning `None` in case of overflow.
#[must_use]
pub fn checked_add(self, other: Self) -> Option<Self> { self.0.checked_add(other.0).map(Self) }
pub fn checked_add(self, other: Self) -> Option<Self> {
self.to_u32().checked_add(other.to_u32()).map(Self)
}
}
impl From<relative::NumberOf512Seconds> for BlockMtpInterval {
@ -458,7 +466,7 @@ crate::internal_macros::impl_sub_assign!(BlockMtpInterval);
impl core::iter::Sum for BlockHeightInterval {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sum = iter.map(|interval| interval.0).sum();
let sum = iter.map(BlockHeightInterval::to_u32).sum();
BlockHeightInterval::from_u32(sum)
}
}
@ -468,14 +476,14 @@ impl<'a> core::iter::Sum<&'a BlockHeightInterval> for BlockHeightInterval {
where
I: Iterator<Item = &'a BlockHeightInterval>,
{
let sum = iter.map(|interval| interval.0).sum();
let sum = iter.map(|interval| interval.to_u32()).sum();
BlockHeightInterval::from_u32(sum)
}
}
impl core::iter::Sum for BlockMtpInterval {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sum = iter.map(|interval| interval.0).sum();
let sum = iter.map(BlockMtpInterval::to_u32).sum();
BlockMtpInterval::from_u32(sum)
}
}
@ -485,7 +493,7 @@ impl<'a> core::iter::Sum<&'a BlockMtpInterval> for BlockMtpInterval {
where
I: Iterator<Item = &'a BlockMtpInterval>,
{
let sum = iter.map(|interval| interval.0).sum();
let sum = iter.map(|interval| interval.to_u32()).sum();
BlockMtpInterval::from_u32(sum)
}
}