Skip to content

refactor: remove magic numbers#64

Open
Alan632 wants to merge 10 commits into
microsoft:mainfrom
Alan632:echo_driver-remove_magic_numbers
Open

refactor: remove magic numbers#64
Alan632 wants to merge 10 commits into
microsoft:mainfrom
Alan632:echo_driver-remove_magic_numbers

Conversation

@Alan632

@Alan632 Alan632 commented Jun 10, 2026

Copy link
Copy Markdown

Summary

Decompose magic numbers into const's adding relevant documentation.

@Alan632 Alan632 self-assigned this Jun 10, 2026
Comment thread general/echo/kmdf/driver/DriverSync/src/queue.rs Outdated
Comment thread general/echo/kmdf/exe/src/main.rs Outdated
Comment thread general/echo/kmdf/driver/DriverSync/src/device.rs Outdated
Comment thread general/echo/kmdf/driver/DriverSync/src/queue.rs Outdated
wmmc88
wmmc88 previously approved these changes Jun 24, 2026
clippy::cast_possible_truncation,
reason = "100ms in 100-nanosecond units is known to fit in i64"
)]
const WDF_REL_TIMEOUT_100_MS: i64 = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's was initially unclear to me what this value is supposed to be for. It would be nice to have it so I didn't need to look at https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdftimer/nf-wdftimer-wdftimerstart docs to understand what value this is. Some things to consider are better name (WDF_TIMER_DUE_TIME_100_MS so its clear this is a value used for wdf timers), SYSTEM_TIME_UNITS instead of UNITS with a short comment about what system time units are (100-nanosecond intervals)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

const {
assert!(UNITS <= i64::MAX as u128, "1,000,000 should fit in i64");
};
-(UNITS as i64)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your negative sign comment should be here, not in the docstring for the value

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

const WDF_REL_TIMEOUT_100_MS: i64 = {
const UNITS: u128 = Duration::from_millis(100).as_nanos() / 100;
const {
assert!(UNITS <= i64::MAX as u128, "1,000,000 should fit in i64");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
assert!(UNITS <= i64::MAX as u128, "1,000,000 should fit in i64");
assert!(UNITS <= i64::MAX as u128, "UNITS should fit in i64");

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to naming convention mentioned above: SYSTEM_TIME_UNITS

unsafe {
(*request_context).cancel_completion_ownership_count = AtomicI32::new(1);
(*request_context).cancel_completion_ownership_count =
AtomicI32::new(INITIAL_CANCEL_OWNERSHIP_COUNT);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine to keep as literal

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted change back to literal

const TIMER_PERIOD_10_S: u32 = {
const MILLIS: u128 = Duration::from_secs(10).as_millis();
const {
assert!(MILLIS <= u32::MAX as u128, "10,000 should fit in u32");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to a literal with a comment

(*request_context)
.cancel_completion_ownership_count
.fetch_sub(2, Ordering::SeqCst);
.fetch_sub(TIMER_CLAIMED_OWNERSHIP_COUNT, Ordering::SeqCst);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this is to make code more readable. I'm not sure this one does that. I think just cleaning up the comment to be clearer and a 2 should be fine for this one

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

- revert unnecessary const's to literals w/improved comments
Size: WDF_TIMER_CONFIG_SIZE,
EvtTimerFunc: Some(echo_evt_timer_func),
Period: TIMER_PERIOD,
Period: 10_000, // 10 seconds, in milliseconds

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a constant here as well?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was requested to keep these as literals:
#64 (comment)
#64 (comment)

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.

3 participants