From f732b1d3cc2b3f9d012b3eeecc70d007666d4f1d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 8 May 2025 10:20:27 +1000 Subject: [PATCH] units: Use functional style This is Rust not C, use functional style. Close: #4464 --- units/src/block.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/units/src/block.rs b/units/src/block.rs index 778fb1bea..83770a5fa 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -158,12 +158,9 @@ impl TryFrom 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 { - 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())) } }