From a2bcbaac52ba08f6297f2980e6180c8a507cdfb4 Mon Sep 17 00:00:00 2001 From: Jimmy Shi Date: Wed, 25 Feb 2026 11:10:25 +0800 Subject: [PATCH] Refactor KRandom implementations using declarative macro Replace 12 nearly identical trait implementations (120+ lines) with a single declarative macro invocation, reducing code to ~25 lines. Benefits: - Eliminates code duplication (DRY principle) - Easier to maintain and extend - Consistent implementation across all integer types - Uses core::mem::size_of for all types (more robust than hardcoded sizes) The macro generates identical code for all primitive integer types: u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize No functional changes - all implementations behave exactly as before. --- crates/zeroos-foundation/src/kfn/random.rs | 140 ++++----------------- 1 file changed, 23 insertions(+), 117 deletions(-) diff --git a/crates/zeroos-foundation/src/kfn/random.rs b/crates/zeroos-foundation/src/kfn/random.rs index c069c99..71766b5 100644 --- a/crates/zeroos-foundation/src/kfn/random.rs +++ b/crates/zeroos-foundation/src/kfn/random.rs @@ -33,120 +33,26 @@ pub trait KRandom: Sized { fn random() -> Self; } -impl KRandom for u8 { - #[inline] - fn random() -> Self { - let mut val = 0u8; - unsafe { krandom(&mut val as *mut u8, 1) }; - val - } -} - -impl KRandom for u16 { - #[inline] - fn random() -> Self { - let mut val = 0u16; - unsafe { krandom(&mut val as *mut u16 as *mut u8, 2) }; - val - } -} - -impl KRandom for u32 { - #[inline] - fn random() -> Self { - let mut val = 0u32; - unsafe { krandom(&mut val as *mut u32 as *mut u8, 4) }; - val - } -} - -impl KRandom for u64 { - #[inline] - fn random() -> Self { - let mut val = 0u64; - unsafe { krandom(&mut val as *mut u64 as *mut u8, 8) }; - val - } -} - -impl KRandom for u128 { - #[inline] - fn random() -> Self { - let mut val = 0u128; - unsafe { krandom(&mut val as *mut u128 as *mut u8, 16) }; - val - } -} - -impl KRandom for usize { - #[inline] - fn random() -> Self { - let mut val = 0usize; - unsafe { - krandom( - &mut val as *mut usize as *mut u8, - core::mem::size_of::(), - ) - }; - val - } -} - -impl KRandom for i8 { - #[inline] - fn random() -> Self { - let mut val = 0i8; - unsafe { krandom(&mut val as *mut i8 as *mut u8, 1) }; - val - } -} - -impl KRandom for i16 { - #[inline] - fn random() -> Self { - let mut val = 0i16; - unsafe { krandom(&mut val as *mut i16 as *mut u8, 2) }; - val - } -} - -impl KRandom for i32 { - #[inline] - fn random() -> Self { - let mut val = 0i32; - unsafe { krandom(&mut val as *mut i32 as *mut u8, 4) }; - val - } -} - -impl KRandom for i64 { - #[inline] - fn random() -> Self { - let mut val = 0i64; - unsafe { krandom(&mut val as *mut i64 as *mut u8, 8) }; - val - } -} - -impl KRandom for i128 { - #[inline] - fn random() -> Self { - let mut val = 0i128; - unsafe { krandom(&mut val as *mut i128 as *mut u8, 16) }; - val - } -} - -impl KRandom for isize { - #[inline] - fn random() -> Self { - let mut val = 0isize; - unsafe { - krandom( - &mut val as *mut isize as *mut u8, - core::mem::size_of::(), - ) - }; - val - } -} +/// Macro to implement KRandom trait for integer types. +/// Reduces boilerplate from 12 separate implementations to a single macro invocation. +macro_rules! impl_krandom { + ($($t:ty),* $(,)?) => { + $( + impl KRandom for $t { + #[inline] + fn random() -> Self { + let mut val: $t = 0; + unsafe { + krandom( + &mut val as *mut $t as *mut u8, + core::mem::size_of::<$t>(), + ) + }; + val + } + } + )* + }; +} + +impl_krandom!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);