diff --git a/units/src/block.rs b/units/src/block.rs index 57cae8ed7..4192f4bb1 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -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 { 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.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 median‐time‐past 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