From cc66838e804a6ceed6e1ac596727db948c9bda08 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 3 Mar 2025 13:05:35 +1100 Subject: [PATCH 1/4] units: Remove unnecessary code comments These comments to not add much value - remove them. --- units/src/block.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/units/src/block.rs b/units/src/block.rs index 49bd88ac9..9e8475db4 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -44,11 +44,9 @@ impl BlockHeight { pub const MAX: Self = BlockHeight(u32::MAX); /// Constructs a new block height from a `u32`. - // Because From 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 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 } } From f5e17914b6698e4b08e80aef7f2276e6897b0843 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 3 Mar 2025 13:09:10 +1100 Subject: [PATCH 2/4] Move Assign impls together Next patch will move all the impls of `Add` and `Sub` into a macro call. In order to make that patch smaller move the assign impls to be together below the add/sub impls. Code move only, no logic change. --- units/src/block.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/units/src/block.rs b/units/src/block.rs index 9e8475db4..af4d30b41 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -211,10 +211,6 @@ impl ops::Add for BlockInterval { } } -impl ops::AddAssign for BlockInterval { - fn add_assign(&mut self, rhs: BlockInterval) { self.0 = self.to_u32() + rhs.to_u32(); } -} - // interval - interval = interval impl ops::Sub for BlockInterval { type Output = BlockInterval; @@ -225,6 +221,10 @@ impl ops::Sub for BlockInterval { } } +impl ops::AddAssign for BlockInterval { + fn add_assign(&mut self, rhs: BlockInterval) { self.0 = self.to_u32() + rhs.to_u32(); } +} + impl ops::SubAssign for BlockInterval { fn sub_assign(&mut self, rhs: BlockInterval) { self.0 = self.to_u32() - rhs.to_u32(); } } From 9d559229529790da70d7342b5566294bbf162650 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 3 Mar 2025 13:11:19 +1100 Subject: [PATCH 3/4] Use impl_op_for_references for block height/interval We have a new macro for implementing ops with a bunch of reference combos. Lets use it for block `Height` and `Interval`. This patch is strictly additive. --- units/src/block.rs | 70 ++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/units/src/block.rs b/units/src/block.rs index af4d30b41..a402853fd 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -171,53 +171,55 @@ impl fmt::Display for TooBigForRelativeBlockHeightError { #[cfg(feature = "std")] impl std::error::Error for TooBigForRelativeBlockHeightError {} -// height - height = interval -impl ops::Sub for BlockHeight { - type Output = BlockInterval; +crate::internal_macros::impl_op_for_references! { + // height - height = interval + impl ops::Sub 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 for BlockHeight { - type Output = BlockHeight; + // height + interval = height + impl ops::Add for BlockHeight { + type Output = BlockHeight; - fn add(self, rhs: BlockInterval) -> Self::Output { - let height = self.to_u32() + rhs.to_u32(); - BlockHeight::from_u32(height) + 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 for BlockHeight { - type Output = BlockHeight; + // height - interval = height + impl ops::Sub for BlockHeight { + type Output = BlockHeight; - fn sub(self, rhs: BlockInterval) -> Self::Output { - let height = self.to_u32() - rhs.to_u32(); - BlockHeight::from_u32(height) + 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 for BlockInterval { - type Output = BlockInterval; + // interval + interval = interval + impl ops::Add for BlockInterval { + type Output = BlockInterval; - fn add(self, rhs: BlockInterval) -> Self::Output { - let height = self.to_u32() + rhs.to_u32(); - BlockInterval::from_u32(height) + 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 for BlockInterval { - type Output = BlockInterval; + // interval - interval = interval + impl ops::Sub for BlockInterval { + type Output = BlockInterval; - fn sub(self, rhs: BlockInterval) -> Self::Output { - let height = self.to_u32() - rhs.to_u32(); - BlockInterval::from_u32(height) + fn sub(self, rhs: BlockInterval) -> Self::Output { + let height = self.to_u32() - rhs.to_u32(); + BlockInterval::from_u32(height) + } } } From 3ae21d5111444f5e01f6cfb1a2b9b314f66418a3 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 3 Mar 2025 13:16:10 +1100 Subject: [PATCH 4/4] Use impl_add/sub_assign for block interval We have a macro for implementing `AddAssign` and `SubAssign`. Use the macro, gets us an additional ref impl for both. --- units/src/block.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/units/src/block.rs b/units/src/block.rs index a402853fd..5d0dc90b8 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -223,13 +223,8 @@ crate::internal_macros::impl_op_for_references! { } } -impl ops::AddAssign for BlockInterval { - fn add_assign(&mut self, rhs: BlockInterval) { self.0 = self.to_u32() + rhs.to_u32(); } -} - -impl ops::SubAssign 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>(iter: I) -> Self {