Skip to content

Commit 6c228a3

Browse files
committed
Merge rust-bitcoin#4507: units: Move code out of wrapper macro
2a3e606 units: Move some things out of impl_u32_macro (Tobin C. Harding) Pull request description: 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. ACKs for top commit: apoelstra: ACK 2a3e606; successfully ran local tests Tree-SHA512: ad4bdbb35bc674e9664e293841e14dc2374c8baddf3e795edb666c75860f02728e939ef5a93ede6f4c951e92c5dd5368d6a6b9662cf6d5b268f73ab5ac97e2cc
2 parents f0302f2 + 2a3e606 commit 6c228a3

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

units/src/block.rs

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,6 @@ macro_rules! impl_u32_wrapper {
3030
$(#[$($type_attrs)*])*
3131
$type_vis struct $newtype($inner_vis u32);
3232

33-
impl $newtype {
34-
/// Block height 0, the genesis block.
35-
pub const ZERO: Self = Self(0);
36-
37-
/// The minimum block height (0), the genesis block.
38-
pub const MIN: Self = Self::ZERO;
39-
40-
/// The maximum block height.
41-
pub const MAX: Self = Self(u32::MAX);
42-
43-
/// Constructs a new block height from a `u32`.
44-
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
45-
46-
/// Returns block height as a `u32`.
47-
pub const fn to_u32(self) -> u32 { self.0 }
48-
}
49-
5033
impl fmt::Display for $newtype {
5134
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
5235
}
@@ -89,6 +72,21 @@ impl_u32_wrapper! {
8972
}
9073

9174
impl BlockHeight {
75+
/// Block height 0, the genesis block.
76+
pub const ZERO: Self = Self(0);
77+
78+
/// The minimum block height (0), the genesis block.
79+
pub const MIN: Self = Self::ZERO;
80+
81+
/// The maximum block height.
82+
pub const MAX: Self = Self(u32::MAX);
83+
84+
/// Constructs a new block height from a `u32`.
85+
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
86+
87+
/// Returns block height as a `u32`.
88+
pub const fn to_u32(self) -> u32 { self.0 }
89+
9290
/// Attempt to subtract two [`BlockHeight`]s, returning `None` in case of overflow.
9391
pub fn checked_sub(self, other: Self) -> Option<BlockHeightInterval> {
9492
self.0.checked_sub(other.0).map(BlockHeightInterval)
@@ -133,6 +131,21 @@ impl_u32_wrapper! {
133131
}
134132

135133
impl BlockHeightInterval {
134+
/// Block interval 0.
135+
pub const ZERO: Self = Self(0);
136+
137+
/// The minimum block interval, equivalent to `Self::ZERO`.
138+
pub const MIN: Self = Self::ZERO;
139+
140+
/// The maximum block interval.
141+
pub const MAX: Self = Self(u32::MAX);
142+
143+
/// Constructs a new block interval from a `u32`.
144+
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
145+
146+
/// Returns block interval as a `u32`.
147+
pub const fn to_u32(self) -> u32 { self.0 }
148+
136149
/// Attempt to subtract two [`BlockHeightInterval`]s, returning `None` in case of overflow.
137150
pub fn checked_sub(self, other: Self) -> Option<Self> { self.0.checked_sub(other.0).map(Self) }
138151

@@ -175,6 +188,24 @@ impl_u32_wrapper! {
175188
}
176189

177190
impl BlockMtp {
191+
/// Block MTP 0.
192+
///
193+
/// Since MTP is a timestamp, 0 is before Bitcoin was invented. This const may still be useful
194+
/// for some use cases e.g., folding a sum of intervals.
195+
pub const ZERO: Self = Self(0);
196+
197+
/// The minimum block MTP, equivalent to `Self::ZERO`.
198+
pub const MIN: Self = Self::ZERO;
199+
200+
/// The maximum block MTP.
201+
pub const MAX: Self = Self(u32::MAX);
202+
203+
/// Constructs a new block MTP from a `u32`.
204+
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
205+
206+
/// Returns block MTP as a `u32`.
207+
pub const fn to_u32(self) -> u32 { self.0 }
208+
178209
/// Constructs a [`BlockMtp`] by computing the median‐time‐past from the last 11 block timestamps
179210
///
180211
/// Because block timestamps are not monotonic, this function internally sorts them;
@@ -229,6 +260,21 @@ impl_u32_wrapper! {
229260
}
230261

231262
impl BlockMtpInterval {
263+
/// Block MTP interval 0.
264+
pub const ZERO: Self = Self(0);
265+
266+
/// The minimum block MTP interval, equivalent to `Self::ZERO`.
267+
pub const MIN: Self = Self::ZERO;
268+
269+
/// The maximum block MTP interval.
270+
pub const MAX: Self = Self(u32::MAX);
271+
272+
/// Constructs a new block MTP interval from a `u32`.
273+
pub const fn from_u32(inner: u32) -> Self { Self(inner) }
274+
275+
/// Returns block MTP interval as a `u32`.
276+
pub const fn to_u32(self) -> u32 { self.0 }
277+
232278
/// Converts a [`BlockMtpInterval`] to a [`locktime::relative::NumberOf512Seconds`], rounding down.
233279
///
234280
/// Relative timelock MTP intervals have a resolution of 512 seconds, while

0 commit comments

Comments
 (0)