units: Use functional style

This is Rust not C, use functional style.

Close: #4464
This commit is contained in:
Tobin C. Harding 2025-05-08 10:20:27 +10:00
parent e43c574146
commit f732b1d3cc
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 3 additions and 6 deletions

View File

@ -158,12 +158,9 @@ impl TryFrom<BlockHeightInterval> for relative::NumberOfBlocks {
/// A relative locktime block height has a maximum value of `u16::MAX` where as a
/// [`BlockHeightInterval`] is a thin wrapper around a `u32`, the two types are not interchangeable.
fn try_from(h: BlockHeightInterval) -> Result<Self, Self::Error> {
let h = h.to_u32();
if h > u32::from(u16::MAX) {
return Err(TooBigForRelativeHeightError(h));
}
Ok(relative::NumberOfBlocks::from(h as u16)) // Cast ok, value checked above.
u16::try_from(h.to_u32())
.map(relative::NumberOfBlocks::from)
.map_err(|_| TooBigForRelativeHeightError(h.into()))
}
}