From 64e10686dee5d0c91053cbac73181f442156a985 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 19 Jun 2025 12:59:03 +1000 Subject: [PATCH] Improve rustdoc examples for absolute locktime Improve the rustdoc examples and fleshing them out, using non-deprecated functions, and returning an error if required (so we can use `?`). --- units/src/locktime/absolute.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/units/src/locktime/absolute.rs b/units/src/locktime/absolute.rs index 73102af8d..50d08ae83 100644 --- a/units/src/locktime/absolute.rs +++ b/units/src/locktime/absolute.rs @@ -65,11 +65,12 @@ impl Height { /// # Examples /// /// ```rust - /// use bitcoin_units::locktime::absolute::Height; + /// use bitcoin_units::locktime::absolute; /// /// let h: u32 = 741521; - /// let height = Height::from_u32(h).expect("invalid height value"); - /// assert_eq!(height.to_consensus_u32(), h); + /// let height = absolute::Height::from_u32(h)?; + /// assert_eq!(height.to_u32(), h); + /// # Ok::<_, absolute::ConversionError>(()) /// ``` #[inline] pub const fn from_u32(n: u32) -> Result { @@ -81,6 +82,14 @@ impl Height { } /// Converts this [`Height`] to a raw `u32` value. + /// + /// # Examples + /// + /// ```rust + /// use bitcoin_units::locktime::absolute; + /// + /// assert_eq!(absolute::Height::MAX.to_u32(), 499_999_999); + /// ``` #[inline] pub const fn to_u32(self) -> u32 { self.0 } @@ -189,11 +198,12 @@ impl MedianTimePast { /// # Examples /// /// ```rust - /// use bitcoin_units::locktime::absolute::MedianTimePast; + /// use bitcoin_units::locktime::absolute; /// /// let t: u32 = 1653195600; // May 22nd, 5am UTC. - /// let time = MedianTimePast::from_u32(t).expect("invalid time value"); - /// assert_eq!(time.to_consensus_u32(), t); + /// let time = absolute::MedianTimePast::from_u32(t)?; + /// assert_eq!(time.to_u32(), t); + /// # Ok::<_, absolute::ConversionError>(()) /// ``` #[inline] pub const fn from_u32(n: u32) -> Result { @@ -205,6 +215,14 @@ impl MedianTimePast { } /// Converts this [`MedianTimePast`] to a raw `u32` value. + /// + /// # Examples + /// + /// ```rust + /// use bitcoin_units::locktime::absolute; + /// + /// assert_eq!(absolute::MedianTimePast::MIN.to_u32(), 500_000_000); + /// ``` #[inline] pub const fn to_u32(self) -> u32 { self.0 }