Add examples to `absolute::LockTime`

This commit is contained in:
Jamil Lambert, PhD 2025-01-28 15:24:15 +00:00
parent 2d73746ad1
commit c16eab7723
No known key found for this signature in database
GPG Key ID: 54DC29234AB5D2C0
1 changed files with 22 additions and 0 deletions

View File

@ -91,12 +91,34 @@ impl LockTime {
pub const SIZE: usize = 4; // Serialized length of a u32.
/// Constructs a new `LockTime` from a prefixed hex string.
///
/// # Examples
///
/// ```
/// # use bitcoin_primitives::absolute::LockTime;
/// let hex_str = "0x61cf9980"; // Unix timestamp for January 1, 2022
/// let lock_time = LockTime::from_hex(hex_str)?;
/// assert_eq!(lock_time.to_consensus_u32(), 0x61cf9980);
///
/// # Ok::<_, units::parse::PrefixedHexError>(())
/// ```
pub fn from_hex(s: &str) -> Result<Self, PrefixedHexError> {
let lock_time = parse::hex_u32_prefixed(s)?;
Ok(Self::from_consensus(lock_time))
}
/// Constructs a new `LockTime` from an unprefixed hex string.
///
/// # Examples
///
/// ```
/// # use bitcoin_primitives::absolute::LockTime;
/// let hex_str = "61cf9980"; // Unix timestamp for January 1, 2022
/// let lock_time = LockTime::from_unprefixed_hex(hex_str)?;
/// assert_eq!(lock_time.to_consensus_u32(), 0x61cf9980);
///
/// # Ok::<_, units::parse::UnprefixedHexError>(())
/// ```
pub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError> {
let lock_time = parse::hex_u32_unprefixed(s)?;
Ok(Self::from_consensus(lock_time))