← LeaderboardThe Skill Leaderboard
rust-binary-record-byte-order
Decode fixed-width Rust binary records with the format's byte order. Diagnose bytemuck alignment errors and verify unaligned payloads and partial words.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-binary-record-byte-orderdescription: Decode fixed-width Rust binary records with the format's byte order. Diagnose bytemuck alignment errors and verify unaligned payloads and partial words.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-03"author: ShianMiketags: [rust, bytemuck, byte-order, alignment, binary-format]Rust binary record byte order and alignment
When to Use
- Casting a byte slice to
&[u32]starts failing when a header or prefix shifts the payload. - A fixed-width binary format decodes to swapped values despite having the expected record count.
- A parser needs to reject a partial final word and accept a valid empty payload according to its format.
Prerequisites
- Obtain the format's word width, byte order, header layout, and trailing-byte policy.
- Identify whether the existing reader borrows a native-memory view, copies bytes, or explicitly decodes the file format.
- Use recognizable asymmetric words and a deterministic aligned and unaligned fixture. The example uses bytemuck 1.25.2 to test native casting.
1. Check width, alignment, and byte order
- Trace how the payload slice is selected. A header can shift an otherwise aligned start address.
- Check width and trailing bytes against the format. A type cast can fail when the resulting bytes do not fill whole target elements.
- For a native borrowed slice, check the start address against the target type's alignment. Safe
bytemuck::try_cast_slicereports a layout error instead of constructing a misaligned borrow. - Independently check byte order. An aligned cast reads values in the current target's native order. It does not convert fixed-endian data.
2. Decode each complete word in format order
- Select the payload with the parser's existing bounds checks. For a format with fixed four-byte words, iterate using
chunks_exact(4)or a supported equivalent. - Inspect the remainder before accepting the result. Return a typed error for a partial word when the format forbids it. Skip padding only when its layout is part of the format.
- Convert each complete byte array with
u32::from_be_bytesoru32::from_le_bytes, according to the format. This works when the payload starts at an arbitrary byte address. - If a documented native-layout fast path is necessary, prove the alignment, length, element validity, and native-endian assumptions at the call site. Keep a tested decoding fallback for other inputs.
3. Validate the actual read boundary
- Use known values with distinct bytes and assert the exact decoded words, not the record count alone.
- Test both an aligned buffer and an offset payload. If the target requires alignment, assert that the test fixture actually creates a misaligned address.
- Exercise an empty payload and a valid prefix followed by a partial word. Assert a structured error for the latter.
- Add an integration check that walks through real header and span parsing when changing a complete reader. Measure the production workload before making a performance claim.
Example
Decode an offset word payload uses big-endian words on little-endian Windows. A cast fails on an offset slice and returns byte-swapped values on an aligned copy. Explicit decoding returns the expected words and rejects a partial payload.
Limitations
- The example verifies a payload of fixed-width unsigned integers. Full framing, embedded struct padding, and other field types need their own validation.
- The aligned native-view comparison runs on little-endian targets. A big-endian native view of these bytes can agree with the format while still requiring valid alignment.
- The decoding function collects into an owned vector. A streaming parser or a known upper allocation limit may need another output strategy.
Safety and Side Effects
Use safe decoding for external record formats and keep example data synthetic. The example allocates a small vector and runs in a scratch crate. It reads and writes no external files.
Related Skills and Documentation
- Rust Pro covers broader Rust engineering practices.
- Systems Programming Rust Project covers general Rust project workflows.
- Bytemuck try_cast_slice documents alignment and slice size constraints.
- Rust u32 byte conversions describe explicit byte order.