From 4786093f61b77ddad2ea87a398b1207a51b7ec02 Mon Sep 17 00:00:00 2001 From: Abdelilah SADIQUI <143453064+sadiqui@users.noreply.github.com> Date: Tue, 12 Aug 2025 22:57:40 +0100 Subject: [PATCH] refactor(solutions/lalgebra_scalar): avoid repetitive implementation code by using a macro --- solutions/lalgebra_scalar/src/lib.rs | 72 ++++++---------------------- 1 file changed, 15 insertions(+), 57 deletions(-) diff --git a/solutions/lalgebra_scalar/src/lib.rs b/solutions/lalgebra_scalar/src/lib.rs index 199be495..aba6d55b 100644 --- a/solutions/lalgebra_scalar/src/lib.rs +++ b/solutions/lalgebra_scalar/src/lib.rs @@ -14,71 +14,29 @@ use std::ops::{Add, Div, Mul, Sub}; -pub trait Scalar: Add + Div + Mul + Sub + std::marker::Sized + Clone { +pub trait Scalar: Add + Div + Mul + Sub + Sized + Clone { type Item; fn zero() -> Self::Item; fn one() -> Self::Item; } -impl Scalar for u32 { - type Item = u32; - fn zero() -> Self::Item { - 0 as u32 - } - fn one() -> Self::Item { - 1 as u32 - } -} - -impl Scalar for u64 { - type Item = u64; - fn zero() -> Self::Item { - 0 as u64 - } - fn one() -> Self::Item { - 1 as u64 - } -} - -impl Scalar for i32 { - type Item = i32; - fn zero() -> Self::Item { - 0 as i32 - } - fn one() -> Self::Item { - 1 as i32 - } +macro_rules! impl_scalar { + ($type:ty, $zero:expr, $one:expr) => { + impl Scalar for $type { + type Item = $type; + fn zero() -> Self::Item { $zero } + fn one() -> Self::Item { $one } + } + }; } -impl Scalar for i64 { - type Item = i64; - fn zero() -> Self::Item { - 0 as i64 - } - fn one() -> Self::Item { - 1 as i64 - } -} - -impl Scalar for f32 { - type Item = f32; - fn zero() -> Self::Item { - 0.0 - } - fn one() -> Self::Item { - 1.0 - } -} +impl_scalar!(u32, 0, 1); +impl_scalar!(u64, 0, 1); +impl_scalar!(i32, 0, 1); +impl_scalar!(i64, 0, 1); +impl_scalar!(f32, 0.0, 1.0); +impl_scalar!(f64, 0.0, 1.0); -impl Scalar for f64 { - type Item = f64; - fn zero() -> Self::Item { - 0.0 - } - fn one() -> Self::Item { - 1.0 - } -} #[cfg(test)] mod test { use super::*;