Skip to content

Commit c4087a8

Browse files
Jimmy Shizouguangxian
authored andcommitted
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.
1 parent 3b132ce commit c4087a8

File tree

1 file changed

+23
-117
lines changed

1 file changed

+23
-117
lines changed

crates/zeroos-foundation/src/kfn/random.rs

Lines changed: 23 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -33,120 +33,26 @@ pub trait KRandom: Sized {
3333
fn random() -> Self;
3434
}
3535

36-
impl KRandom for u8 {
37-
#[inline]
38-
fn random() -> Self {
39-
let mut val = 0u8;
40-
unsafe { krandom(&mut val as *mut u8, 1) };
41-
val
42-
}
43-
}
44-
45-
impl KRandom for u16 {
46-
#[inline]
47-
fn random() -> Self {
48-
let mut val = 0u16;
49-
unsafe { krandom(&mut val as *mut u16 as *mut u8, 2) };
50-
val
51-
}
52-
}
53-
54-
impl KRandom for u32 {
55-
#[inline]
56-
fn random() -> Self {
57-
let mut val = 0u32;
58-
unsafe { krandom(&mut val as *mut u32 as *mut u8, 4) };
59-
val
60-
}
61-
}
62-
63-
impl KRandom for u64 {
64-
#[inline]
65-
fn random() -> Self {
66-
let mut val = 0u64;
67-
unsafe { krandom(&mut val as *mut u64 as *mut u8, 8) };
68-
val
69-
}
70-
}
71-
72-
impl KRandom for u128 {
73-
#[inline]
74-
fn random() -> Self {
75-
let mut val = 0u128;
76-
unsafe { krandom(&mut val as *mut u128 as *mut u8, 16) };
77-
val
78-
}
79-
}
80-
81-
impl KRandom for usize {
82-
#[inline]
83-
fn random() -> Self {
84-
let mut val = 0usize;
85-
unsafe {
86-
krandom(
87-
&mut val as *mut usize as *mut u8,
88-
core::mem::size_of::<usize>(),
89-
)
90-
};
91-
val
92-
}
93-
}
94-
95-
impl KRandom for i8 {
96-
#[inline]
97-
fn random() -> Self {
98-
let mut val = 0i8;
99-
unsafe { krandom(&mut val as *mut i8 as *mut u8, 1) };
100-
val
101-
}
102-
}
103-
104-
impl KRandom for i16 {
105-
#[inline]
106-
fn random() -> Self {
107-
let mut val = 0i16;
108-
unsafe { krandom(&mut val as *mut i16 as *mut u8, 2) };
109-
val
110-
}
111-
}
112-
113-
impl KRandom for i32 {
114-
#[inline]
115-
fn random() -> Self {
116-
let mut val = 0i32;
117-
unsafe { krandom(&mut val as *mut i32 as *mut u8, 4) };
118-
val
119-
}
120-
}
121-
122-
impl KRandom for i64 {
123-
#[inline]
124-
fn random() -> Self {
125-
let mut val = 0i64;
126-
unsafe { krandom(&mut val as *mut i64 as *mut u8, 8) };
127-
val
128-
}
129-
}
130-
131-
impl KRandom for i128 {
132-
#[inline]
133-
fn random() -> Self {
134-
let mut val = 0i128;
135-
unsafe { krandom(&mut val as *mut i128 as *mut u8, 16) };
136-
val
137-
}
138-
}
139-
140-
impl KRandom for isize {
141-
#[inline]
142-
fn random() -> Self {
143-
let mut val = 0isize;
144-
unsafe {
145-
krandom(
146-
&mut val as *mut isize as *mut u8,
147-
core::mem::size_of::<isize>(),
148-
)
149-
};
150-
val
151-
}
152-
}
36+
/// Macro to implement KRandom trait for integer types.
37+
/// Reduces boilerplate from 12 separate implementations to a single macro invocation.
38+
macro_rules! impl_krandom {
39+
($($t:ty),* $(,)?) => {
40+
$(
41+
impl KRandom for $t {
42+
#[inline]
43+
fn random() -> Self {
44+
let mut val: $t = 0;
45+
unsafe {
46+
krandom(
47+
&mut val as *mut $t as *mut u8,
48+
core::mem::size_of::<$t>(),
49+
)
50+
};
51+
val
52+
}
53+
}
54+
)*
55+
};
56+
}
57+
58+
impl_krandom!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

0 commit comments

Comments
 (0)