units: Move some things out of impl_u32_macro

Recently we added a private `impl_u32_macro`. It included a bunch of
associated consts and a pair of u32 constructor/getter functions.

We overlooked the fact that the macro produces incorrect docs.

Move the offending code out of the macro and into the already existent
impl block for each type.

Docs only, no other logic change.
This commit is contained in:
Tobin C. Harding 2025-05-27 08:36:18 +01:00
parent 5e0b86d2b1
commit 2a3e606d89
No known key found for this signature in database
GPG Key ID: 0AEF0A899E41F7DD
1 changed files with 63 additions and 17 deletions

View File

@ -30,23 +30,6 @@ macro_rules! impl_u32_wrapper {
$(#[$($type_attrs)*])*
$type_vis struct $newtype($inner_vis u32);
impl $newtype {
/// Block height 0, the genesis block.
pub const ZERO: Self = Self(0);
/// The minimum block height (0), the genesis block.
pub const MIN: Self = Self::ZERO;
/// The maximum block height.
pub const MAX: Self = Self(u32::MAX);
/// Constructs a new block height from a `u32`.
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
/// Returns block height as a `u32`.
pub const fn to_u32(self) -> u32 { self.0 }
}
impl fmt::Display for $newtype {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}
@ -89,6 +72,21 @@ impl_u32_wrapper! {
}
impl BlockHeight {
/// Block height 0, the genesis block.
pub const ZERO: Self = Self(0);
/// The minimum block height (0), the genesis block.
pub const MIN: Self = Self::ZERO;
/// The maximum block height.
pub const MAX: Self = Self(u32::MAX);
/// Constructs a new block height from a `u32`.
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
/// Returns block height as a `u32`.
pub const fn to_u32(self) -> u32 { self.0 }
/// Attempt to subtract two [`BlockHeight`]s, returning `None` in case of overflow.
pub fn checked_sub(self, other: Self) -> Option<BlockHeightInterval> {
self.0.checked_sub(other.0).map(BlockHeightInterval)
@ -133,6 +131,21 @@ impl_u32_wrapper! {
}
impl BlockHeightInterval {
/// Block interval 0.
pub const ZERO: Self = Self(0);
/// The minimum block interval, equivalent to `Self::ZERO`.
pub const MIN: Self = Self::ZERO;
/// The maximum block interval.
pub const MAX: Self = Self(u32::MAX);
/// Constructs a new block interval from a `u32`.
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
/// Returns block interval as a `u32`.
pub const fn to_u32(self) -> u32 { self.0 }
/// Attempt to subtract two [`BlockHeightInterval`]s, returning `None` in case of overflow.
pub fn checked_sub(self, other: Self) -> Option<Self> { self.0.checked_sub(other.0).map(Self) }
@ -175,6 +188,24 @@ impl_u32_wrapper! {
}
impl BlockMtp {
/// Block MTP 0.
///
/// Since MTP is a timestamp, 0 is before Bitcoin was invented. This const may still be useful
/// for some use cases e.g., folding a sum of intervals.
pub const ZERO: Self = Self(0);
/// The minimum block MTP, equivalent to `Self::ZERO`.
pub const MIN: Self = Self::ZERO;
/// The maximum block MTP.
pub const MAX: Self = Self(u32::MAX);
/// Constructs a new block MTP from a `u32`.
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
/// Returns block MTP as a `u32`.
pub const fn to_u32(self) -> u32 { self.0 }
/// Constructs a [`BlockMtp`] by computing the mediantimepast from the last 11 block timestamps
///
/// Because block timestamps are not monotonic, this function internally sorts them;
@ -229,6 +260,21 @@ impl_u32_wrapper! {
}
impl BlockMtpInterval {
/// Block MTP interval 0.
pub const ZERO: Self = Self(0);
/// The minimum block MTP interval, equivalent to `Self::ZERO`.
pub const MIN: Self = Self::ZERO;
/// The maximum block MTP interval.
pub const MAX: Self = Self(u32::MAX);
/// Constructs a new block MTP interval from a `u32`.
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
/// Returns block MTP interval as a `u32`.
pub const fn to_u32(self) -> u32 { self.0 }
/// Converts a [`BlockMtpInterval`] to a [`locktime::relative::NumberOf512Seconds`], rounding down.
///
/// Relative timelock MTP intervals have a resolution of 512 seconds, while