How to serialize/deserialize a struct to/from [u8], but packed?

I need to serialize a struct to [u8] but remove any gaps between members.

Also, when deserializing [u8] to the struct, any necessary gaps must be added back.

I can define a repr(packed) and a repr(rust) version for this structure, but I have several such kinds of structures. This makes the work rather boring.

Is there any better way to do this?

Or is there a way to automatically generate repr(rust) and repr(packed) versions of a structure?


My final solution:
use packed_struct

Sorry, I accessed the wrong forum…

This seems to be related to rust, not nix… you would surely get more answers on a rust forum/stackoverflow.

Sorry, I accessed the incorrect forum…