Skip to content

refactor(link_layer/pktap): drop no-op byte-order conversions#292

Merged
domcyrus merged 1 commit into
domcyrus:mainfrom
obchain:refactor/pktap-drop-dead-byteorder
May 20, 2026
Merged

refactor(link_layer/pktap): drop no-op byte-order conversions#292
domcyrus merged 1 commit into
domcyrus:mainfrom
obchain:refactor/pktap-drop-dead-byteorder

Conversation

@obchain
Copy link
Copy Markdown
Contributor

@obchain obchain commented May 20, 2026

Summary

Drops five no-op byte-order conversions in PktapHeader::from_bytes (src/network/link_layer/pktap.rs:60-64) and expands the SAFETY comment to spell out the little-endian assumption so the round-trip isn't reintroduced.

Closes #291.

Why

Each of the five lines was header.field = u32::from_le_bytes(header.field.to_le_bytes()) — an identity transform. to_le_bytes serialises the native u32 to little-endian; from_le_bytes reads those bytes back as a u32. Same byte order in, same byte order out, on every platform Rust supports. The comment claimed "Convert from network byte order if needed", but PKTAP is macOS-only, macOS is exclusively little-endian (x86_64 + arm64), and the wire format is little-endian — there is no big-endian path to convert from. The read_unaligned already yields the decoded values directly.

The expanded SAFETY comment now records the LE assumption next to the unsafe block so a future patcher adding another u32 field doesn't reintroduce the dead from_le_bytes(x.to_le_bytes()) pattern.

Test plan

  • cargo clippy --all-targets --all-features -- -D warnings — clean
  • cargo fmt --check — clean
  • cargo test --lib — 361 passed, 0 failed
  • Manually verified: the existing test_pktap_header_size, test_invalid_pktap_data tests in the same file still cover the parse path

Five `u32::from_le_bytes(x.to_le_bytes())` lines after the
`read_unaligned` were identity transforms — `to_le_bytes` produces
little-endian bytes and `from_le_bytes` interprets them as a native
u32, so the round-trip is a no-op on every supported platform. PKTAP
is macOS-only and macOS is little-endian, so the struct read already
yields the correct field values; the conversions were never doing
work.

Drop them and update the SAFETY comment to spell out the endianness
assumption so a future reader doesn't reintroduce the dead code.
@domcyrus
Copy link
Copy Markdown
Owner

@obchain Thanks a lot for your PR. This LGTM!

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.

refactor(link_layer/pktap): drop no-op byte-order conversions in PktapHeader::from_bytes

2 participants