Struct packing refactor

This refactors the struct packing in a slightly lower-level, more efficient way. This is a low level building block, so it seems appropriate to use more direct knowledge of the struct type to do the packing. Support for packing std::vector is also added.