Skip to content

Big endian support - minor adjustments - #147

Merged
avl merged 6 commits into
masterfrom
big_endian_adjustments
Oct 19, 2026
Merged

avl merged 6 commits into
masterfrom
big_endian_adjustments

Conversation

@avl

@avl avl commented Oct 19, 2026

Copy link
Copy Markdown
Owner

No description provided.

pkubaj and others added 6 commits October 15, 2026 10:49
The Serialize/Deserialize impls for bit_vec::BitVec and bit_set::BitSet
(both the 0.6/0.5 and the 0.8 versions) copy the u32 storage words of
the bit vector to and from the stream as raw memory, which only yields
the little-endian savefile format on little-endian hosts. Instead of
handling that case the crate refused to build on big-endian targets with
a compile_error!, so every dependent crate using the default feature set
failed to compile there (seen with FreeBSD/powerpc64).

Keep the raw memory copy on little-endian hosts. On big-endian hosts,
write the storage words through write_u32 and convert them with
u32::from_le after the bulk read. The on-disk format is unchanged: a
big-endian build now produces byte-identical output to a little-endian
one for both bit-vec and both bit-set versions, and round-trips it
(verified on FreeBSD powerpc64 against powerpc64le).
get_variant_offsets conjures a value of each variant by writing the
discriminant into the first bytes of a zeroed buffer, byte by byte in
little-endian order. On a big-endian host that produces an invalid tag
for u16/u32 discriminants (e.g. 0x01000000 for variant 1), so the
derive's const evaluation of repr_c_optimization_safe fails to compile
for every #[repr(u16)]/#[repr(u32)] enum with fields:

  error[E0080]: enum value has invalid tag: 0x01000000

Write the discriminant with to_ne_bytes instead, which is const and
yields the host's own byte order on either endianness.
The Packed optimization copies the in-memory representation of a type
straight to and from the stream, which equals the little-endian savefile
format only on little-endian hosts (the Packed docs already list "The
host platform is little endian" as a precondition). Nothing enforces
that: the primitive impls answer IsPacked::yes() unconditionally, and
the derive macro writes every run of address-contiguous primitive
fields with raw_write_region() while the generated Deserialize impl
reads them back field by field as little-endian. On a big-endian host
data is silently corrupted on round-trip: a struct { a: f64, b: f64 }
written and read on the same machine returns byte-swapped values, and
20 tests of savefile-test fail that way.

Make IsPacked::yes() yield "no" on big-endian targets, so every fast
path keyed off repr_c_optimization_safe (Vec<T>, arrays, derived types,
manual Packed impls) falls back to the per-field little-endian code, and
gate the derive's contiguous-field raw_write_region() on little-endian
as well, since that one only checks field adjacency. The output of a
big-endian build then becomes byte-identical to a little-endian one.
The compile-time #[savefile_require_fast] layout check only inspects
field offsets and is unaffected.
Master contains the squashed contributor MR (#146), whose tree is
identical to 9dcce88 on this branch. Resolved by keeping big_endian's
versions, which supersede the 'disable Packed on big-endian' approach.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@avl
avl enabled auto-merge (squash) October 19, 2026 21:46
@avl
avl merged commit 5b04105 into master Oct 19, 2026
1 check 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