Merge rust-bitcoin/rust-bitcoin#4507: units: Move code out of wrapper macro
2a3e606d89
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 2a3e606d89d25ce54ff91b38b0aa29f7f733b9d9; successfully ran local tests
Tree-SHA512: ad4bdbb35bc674e9664e293841e14dc2374c8baddf3e795edb666c75860f02728e939ef5a93ede6f4c951e92c5dd5368d6a6b9662cf6d5b268f73ab5ac97e2cc
This commit is contained in:
commit
6c228a3626
|
@ -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 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
|
||||
|
|
Loading…
Reference in New Issue