From 512326b8b997cbdfbbf9746252a2a52264a139f8 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 7 Apr 2025 11:42:03 +1000 Subject: [PATCH] units: Macroize implementing OptionExt We are going to add implementations of `OptionExt` for various other types and all impls are almost identical. To make doing so easier macroize the implementation for `Amount` and `SignedAmount`. Internal change only, no logic changes. --- units/src/result.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/units/src/result.rs b/units/src/result.rs index afe268875..0663a6231 100644 --- a/units/src/result.rs +++ b/units/src/result.rs @@ -110,25 +110,22 @@ pub(crate) trait OptionExt { fn valid_or_error(self) -> NumOpResult; } -impl OptionExt for Option { - #[inline] - fn valid_or_error(self) -> NumOpResult { - match self { - Some(amount) => R::Valid(amount), - None => R::Error(NumOpError {}), - } - } -} - -impl OptionExt for Option { - #[inline] - fn valid_or_error(self) -> NumOpResult { - match self { - Some(amount) => R::Valid(amount), - None => R::Error(NumOpError {}), - } +macro_rules! impl_opt_ext { + ($($ty:ident),* $(,)?) => { + $( + impl OptionExt<$ty> for Option<$ty> { + #[inline] + fn valid_or_error(self) -> NumOpResult<$ty> { + match self { + Some(amount) => R::Valid(amount), + None => R::Error(NumOpError {}), + } + } + } + )* } } +impl_opt_ext!(Amount, SignedAmount); /// An error occurred while doing a mathematical operation. #[derive(Debug, Copy, Clone, PartialEq, Eq)]