Skip to content

refactor(kfn): Refactor KRandom implementations using declarative macro#31

Merged
zouguangxian merged 1 commit intoLayerZero-Labs:mainfrom
JimmyShi22:refactor-krandom-macro
Mar 8, 2026
Merged

refactor(kfn): Refactor KRandom implementations using declarative macro#31
zouguangxian merged 1 commit intoLayerZero-Labs:mainfrom
JimmyShi22:refactor-krandom-macro

Conversation

@JimmyShi22
Copy link

This pull request refactors the implementation of the KRandom trait for integer types in random.rs to significantly reduce boilerplate and improve maintainability. Instead of writing separate implementations for each integer type, a macro is introduced to generate them automatically.

Trait implementation refactoring:

  • Introduced the impl_krandom! macro to implement the KRandom trait for multiple integer types, replacing twelve separate manual implementations with a single macro invocation.
  • Applied the macro to all relevant integer types: u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, and isize.

@cursor
Copy link

cursor bot commented Feb 25, 2026

PR Summary

Low Risk
Pure refactor that centralizes duplicated trait impl code; behavioral risk is minimal aside from potential subtle size/cast mistakes across integer types.

Overview
Refactors KRandom integer implementations in kfn/random.rs by replacing 12 hand-written impl blocks with a single impl_krandom! declarative macro invocation.

The generated impls now uniformly call krandom using core::mem::size_of::<T>(), reducing boilerplate while keeping the same set of supported integer types.

Written by Cursor Bugbot for commit 82be757. This will update automatically on new commits. Configure here.

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.
@zouguangxian zouguangxian force-pushed the refactor-krandom-macro branch from 82be757 to a2bcbaa Compare March 6, 2026 19:01
@zouguangxian zouguangxian merged commit c4087a8 into LayerZero-Labs:main Mar 8, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants