musli
musli copied to clipboard
Musli-wire forces variable-length integer encoding everywhere inside #[musli(packed)]
I was looking into why a #[musli(packed)] struct Point3 { x: f32, y: f32, z: f32 }
was so small when it was all-zeros despite setting the integer encoding to fixed length, and I discovered that ALL fields in packed structs use variable length int encoding. You can't turn it off by choosing fixed length integers in your musli-wire Encoding. Since musli-wire implements packed using musli-storage, I would have thought it would just pass the variable/fixed-ness down to that. But it doesn't: https://github.com/udoprog/musli/blob/be09b1fedef1a4040a1b8028b3eaa714482dc48b/crates/musli-wire/src/en.rs#L397 (also see the decoder using Variable, Variable).
This was introduced in https://github.com/udoprog/musli/commit/7a3c3878d0db2a232d7b06a48c4a77ce7ab702f3
Of course you may not consider this a bug. Packed structs are supposed to be small, gotta save space for only 62 bytes, etc.
For making the behaviour optional, you could add type params to Encoding specifically for ints and lengths in a packed struct. To avoid breaking changes to people's formats, the type parameters could default to Variable
. And then you would pass these down to the StorageEncoder/Decoder instead of always passing Variable.