Skip to content

Commit 64e1068

Browse files
committed
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 `?`).
1 parent 5743a81 commit 64e1068

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

units/src/locktime/absolute.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ impl Height {
6565
/// # Examples
6666
///
6767
/// ```rust
68-
/// use bitcoin_units::locktime::absolute::Height;
68+
/// use bitcoin_units::locktime::absolute;
6969
///
7070
/// let h: u32 = 741521;
71-
/// let height = Height::from_u32(h).expect("invalid height value");
72-
/// assert_eq!(height.to_consensus_u32(), h);
71+
/// let height = absolute::Height::from_u32(h)?;
72+
/// assert_eq!(height.to_u32(), h);
73+
/// # Ok::<_, absolute::ConversionError>(())
7374
/// ```
7475
#[inline]
7576
pub const fn from_u32(n: u32) -> Result<Height, ConversionError> {
@@ -81,6 +82,14 @@ impl Height {
8182
}
8283

8384
/// Converts this [`Height`] to a raw `u32` value.
85+
///
86+
/// # Examples
87+
///
88+
/// ```rust
89+
/// use bitcoin_units::locktime::absolute;
90+
///
91+
/// assert_eq!(absolute::Height::MAX.to_u32(), 499_999_999);
92+
/// ```
8493
#[inline]
8594
pub const fn to_u32(self) -> u32 { self.0 }
8695

@@ -189,11 +198,12 @@ impl MedianTimePast {
189198
/// # Examples
190199
///
191200
/// ```rust
192-
/// use bitcoin_units::locktime::absolute::MedianTimePast;
201+
/// use bitcoin_units::locktime::absolute;
193202
///
194203
/// let t: u32 = 1653195600; // May 22nd, 5am UTC.
195-
/// let time = MedianTimePast::from_u32(t).expect("invalid time value");
196-
/// assert_eq!(time.to_consensus_u32(), t);
204+
/// let time = absolute::MedianTimePast::from_u32(t)?;
205+
/// assert_eq!(time.to_u32(), t);
206+
/// # Ok::<_, absolute::ConversionError>(())
197207
/// ```
198208
#[inline]
199209
pub const fn from_u32(n: u32) -> Result<Self, ConversionError> {
@@ -205,6 +215,14 @@ impl MedianTimePast {
205215
}
206216

207217
/// Converts this [`MedianTimePast`] to a raw `u32` value.
218+
///
219+
/// # Examples
220+
///
221+
/// ```rust
222+
/// use bitcoin_units::locktime::absolute;
223+
///
224+
/// assert_eq!(absolute::MedianTimePast::MIN.to_u32(), 500_000_000);
225+
/// ```
208226
#[inline]
209227
pub const fn to_u32(self) -> u32 { self.0 }
210228

0 commit comments

Comments
 (0)