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.
This commit is contained in:
Tobin C. Harding 2025-04-07 11:42:03 +10:00
parent f5b54e5fe0
commit 512326b8b9
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 14 additions and 17 deletions

View File

@ -110,25 +110,22 @@ pub(crate) trait OptionExt<T> {
fn valid_or_error(self) -> NumOpResult<T>;
}
impl OptionExt<Amount> for Option<Amount> {
#[inline]
fn valid_or_error(self) -> NumOpResult<Amount> {
match self {
Some(amount) => R::Valid(amount),
None => R::Error(NumOpError {}),
}
}
}
impl OptionExt<SignedAmount> for Option<SignedAmount> {
#[inline]
fn valid_or_error(self) -> NumOpResult<SignedAmount> {
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)]