Merge rust-bitcoin/rust-bitcoin#4169: units: Increase ops impls in `block` module

3ae21d5111 Use impl_add/sub_assign for block interval (Tobin C. Harding)
9d55922952 Use impl_op_for_references for block height/interval (Tobin C. Harding)
f5e17914b6 Move Assign impls together (Tobin C. Harding)
cc66838e80 units: Remove unnecessary code comments (Tobin C. Harding)

Pull request description:

  Improve the ops impls in the `block` module using the already present macros.

ACKs for top commit:
  apoelstra:
    ACK 3ae21d5111444f5e01f6cfb1a2b9b314f66418a3; successfully ran local tests; nice!

Tree-SHA512: 6565426a06bb47d337d21cf5c59acca43e69228dbec8319fc95373025d220d8ec6273c54f214f312c4229603c455d08e4c6a8c108663c6db5086df36266979de
This commit is contained in:
merge-script 2025-03-26 02:21:12 +00:00
commit ecffb4071b
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 50 additions and 57 deletions

View File

@ -44,11 +44,9 @@ impl BlockHeight {
pub const MAX: Self = BlockHeight(u32::MAX);
/// Constructs a new block height from a `u32`.
// Because From<u32> is not const.
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
/// Returns block height as a `u32`.
// Because type inference doesn't always work using `Into`.
pub const fn to_u32(self) -> u32 { self.0 }
}
@ -111,11 +109,9 @@ impl BlockInterval {
pub const MAX: Self = BlockInterval(u32::MAX);
/// Constructs a new block interval from a `u32`.
// Because From<u32> is not const.
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
/// Returns block interval as a `u32`.
// Because type inference doesn't always work using `Into`.
pub const fn to_u32(self) -> u32 { self.0 }
}
@ -175,63 +171,60 @@ impl fmt::Display for TooBigForRelativeBlockHeightError {
#[cfg(feature = "std")]
impl std::error::Error for TooBigForRelativeBlockHeightError {}
// height - height = interval
impl ops::Sub<BlockHeight> for BlockHeight {
type Output = BlockInterval;
crate::internal_macros::impl_op_for_references! {
// height - height = interval
impl ops::Sub<BlockHeight> for BlockHeight {
type Output = BlockInterval;
fn sub(self, rhs: BlockHeight) -> Self::Output {
let interval = self.to_u32() - rhs.to_u32();
BlockInterval::from_u32(interval)
fn sub(self, rhs: BlockHeight) -> Self::Output {
let interval = self.to_u32() - rhs.to_u32();
BlockInterval::from_u32(interval)
}
}
// height + interval = height
impl ops::Add<BlockInterval> for BlockHeight {
type Output = BlockHeight;
fn add(self, rhs: BlockInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockHeight::from_u32(height)
}
}
// height - interval = height
impl ops::Sub<BlockInterval> for BlockHeight {
type Output = BlockHeight;
fn sub(self, rhs: BlockInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockHeight::from_u32(height)
}
}
// interval + interval = interval
impl ops::Add<BlockInterval> for BlockInterval {
type Output = BlockInterval;
fn add(self, rhs: BlockInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockInterval::from_u32(height)
}
}
// interval - interval = interval
impl ops::Sub<BlockInterval> for BlockInterval {
type Output = BlockInterval;
fn sub(self, rhs: BlockInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockInterval::from_u32(height)
}
}
}
// height + interval = height
impl ops::Add<BlockInterval> for BlockHeight {
type Output = BlockHeight;
fn add(self, rhs: BlockInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockHeight::from_u32(height)
}
}
// height - interval = height
impl ops::Sub<BlockInterval> for BlockHeight {
type Output = BlockHeight;
fn sub(self, rhs: BlockInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockHeight::from_u32(height)
}
}
// interval + interval = interval
impl ops::Add<BlockInterval> for BlockInterval {
type Output = BlockInterval;
fn add(self, rhs: BlockInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockInterval::from_u32(height)
}
}
impl ops::AddAssign<BlockInterval> for BlockInterval {
fn add_assign(&mut self, rhs: BlockInterval) { self.0 = self.to_u32() + rhs.to_u32(); }
}
// interval - interval = interval
impl ops::Sub<BlockInterval> for BlockInterval {
type Output = BlockInterval;
fn sub(self, rhs: BlockInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockInterval::from_u32(height)
}
}
impl ops::SubAssign<BlockInterval> for BlockInterval {
fn sub_assign(&mut self, rhs: BlockInterval) { self.0 = self.to_u32() - rhs.to_u32(); }
}
crate::internal_macros::impl_add_assign!(BlockInterval);
crate::internal_macros::impl_sub_assign!(BlockInterval);
impl core::iter::Sum for BlockInterval {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {