feat: Create absolute lock time at compile time

Mark the from_consensus and to_consensus methods of the absolute
lock time structs as const. In theory. these methods do some sanity
checking and wrap a u32 value in a newtype. This should be possible
to do in const. Marking the methods as const should not break existing
call sites.
This commit is contained in:
Christian Lewe 2024-09-28 18:05:57 +02:00
parent 10f19683cb
commit 53e1fb6b0c
1 changed files with 8 additions and 8 deletions

View File

@ -62,7 +62,7 @@ impl Height {
/// assert_eq!(height.to_consensus_u32(), h); /// assert_eq!(height.to_consensus_u32(), h);
/// ``` /// ```
#[inline] #[inline]
pub fn from_consensus(n: u32) -> Result<Height, ConversionError> { pub const fn from_consensus(n: u32) -> Result<Height, ConversionError> {
if is_block_height(n) { if is_block_height(n) {
Ok(Self(n)) Ok(Self(n))
} else { } else {
@ -72,7 +72,7 @@ impl Height {
/// Converts this [`Height`] to its inner `u32` value. /// Converts this [`Height`] to its inner `u32` value.
#[inline] #[inline]
pub fn to_consensus_u32(self) -> u32 { self.0 } pub const fn to_consensus_u32(self) -> u32 { self.0 }
} }
impl fmt::Display for Height { impl fmt::Display for Height {
@ -158,7 +158,7 @@ impl Time {
/// assert_eq!(time.to_consensus_u32(), t); /// assert_eq!(time.to_consensus_u32(), t);
/// ``` /// ```
#[inline] #[inline]
pub fn from_consensus(n: u32) -> Result<Time, ConversionError> { pub const fn from_consensus(n: u32) -> Result<Time, ConversionError> {
if is_block_time(n) { if is_block_time(n) {
Ok(Self(n)) Ok(Self(n))
} else { } else {
@ -168,7 +168,7 @@ impl Time {
/// Converts this [`Time`] to its inner `u32` value. /// Converts this [`Time`] to its inner `u32` value.
#[inline] #[inline]
pub fn to_consensus_u32(self) -> u32 { self.0 } pub const fn to_consensus_u32(self) -> u32 { self.0 }
} }
impl fmt::Display for Time { impl fmt::Display for Time {
@ -244,10 +244,10 @@ where
} }
/// Returns true if `n` is a block height i.e., less than 500,000,000. /// Returns true if `n` is a block height i.e., less than 500,000,000.
pub fn is_block_height(n: u32) -> bool { n < LOCK_TIME_THRESHOLD } pub const fn is_block_height(n: u32) -> bool { n < LOCK_TIME_THRESHOLD }
/// Returns true if `n` is a UNIX timestamp i.e., greater than or equal to 500,000,000. /// Returns true if `n` is a UNIX timestamp i.e., greater than or equal to 500,000,000.
pub fn is_block_time(n: u32) -> bool { n >= LOCK_TIME_THRESHOLD } pub const fn is_block_time(n: u32) -> bool { n >= LOCK_TIME_THRESHOLD }
/// An error that occurs when converting a `u32` to a lock time variant. /// An error that occurs when converting a `u32` to a lock time variant.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -261,10 +261,10 @@ pub struct ConversionError {
impl ConversionError { impl ConversionError {
/// Constructs a `ConversionError` from an invalid `n` when expecting a height value. /// Constructs a `ConversionError` from an invalid `n` when expecting a height value.
fn invalid_height(n: u32) -> Self { Self { unit: LockTimeUnit::Blocks, input: n } } const fn invalid_height(n: u32) -> Self { Self { unit: LockTimeUnit::Blocks, input: n } }
/// Constructs a `ConversionError` from an invalid `n` when expecting a time value. /// Constructs a `ConversionError` from an invalid `n` when expecting a time value.
fn invalid_time(n: u32) -> Self { Self { unit: LockTimeUnit::Seconds, input: n } } const fn invalid_time(n: u32) -> Self { Self { unit: LockTimeUnit::Seconds, input: n } }
} }
impl fmt::Display for ConversionError { impl fmt::Display for ConversionError {