diff --git a/bitcoin/src/blockdata/locktime/relative.rs b/bitcoin/src/blockdata/locktime/relative.rs index 70d4b27b..29af016c 100644 --- a/bitcoin/src/blockdata/locktime/relative.rs +++ b/bitcoin/src/blockdata/locktime/relative.rs @@ -70,8 +70,11 @@ impl LockTime { /// /// Will return an error if the input cannot be encoded in 16 bits. #[inline] - pub fn from_seconds_floor(seconds: u32) -> Result { - Time::from_seconds_floor(seconds).map(LockTime::Time) + pub const fn from_seconds_floor(seconds: u32) -> Result { + match Time::from_seconds_floor(seconds) { + Ok(time) => Ok(LockTime::Time(time)), + Err(e) => Err(e), + } } /// Create a [`LockTime`] from seconds, converting the seconds into 512 second interval @@ -81,8 +84,11 @@ impl LockTime { /// /// Will return an error if the input cannot be encoded in 16 bits. #[inline] - pub fn from_seconds_ceil(seconds: u32) -> Result { - Time::from_seconds_ceil(seconds).map(LockTime::Time) + pub const fn from_seconds_ceil(seconds: u32) -> Result { + match Time::from_seconds_ceil(seconds) { + Ok(time) => Ok(LockTime::Time(time)), + Err(e) => Err(e), + } } /// Returns true if this [`relative::LockTime`] is satisfied by either height or time. diff --git a/units/src/locktime/relative.rs b/units/src/locktime/relative.rs index d6b22bcf..5c19794d 100644 --- a/units/src/locktime/relative.rs +++ b/units/src/locktime/relative.rs @@ -72,9 +72,11 @@ impl Time { /// /// Will return an error if the input cannot be encoded in 16 bits. #[inline] - pub fn from_seconds_floor(seconds: u32) -> Result { - if let Ok(interval) = u16::try_from(seconds / 512) { - Ok(Time::from_512_second_intervals(interval)) + #[rustfmt::skip] // moves comments to unrelated code + pub const fn from_seconds_floor(seconds: u32) -> Result { + let interval = seconds / 512; + if interval <= u16::MAX as u32 { // infallible cast, needed by const code + Ok(Time::from_512_second_intervals(interval as u16)) // cast checked above, needed by const code } else { Err(TimeOverflowError { seconds }) } @@ -87,9 +89,11 @@ impl Time { /// /// Will return an error if the input cannot be encoded in 16 bits. #[inline] - pub fn from_seconds_ceil(seconds: u32) -> Result { - if let Ok(interval) = u16::try_from((seconds + 511) / 512) { - Ok(Time::from_512_second_intervals(interval)) + #[rustfmt::skip] // moves comments to unrelated code + pub const fn from_seconds_ceil(seconds: u32) -> Result { + let interval = (seconds + 511) / 512; + if interval <= u16::MAX as u32 { // infallible cast, needed by const code + Ok(Time::from_512_second_intervals(interval as u16)) // cast checked above, needed by const code } else { Err(TimeOverflowError { seconds }) }