3 ms·
Wow, that SO post is full of very bad answers. Regardless ... Even ignoring compiler extensions, you don't have to memcpy the struct to the network directly (t
by o11c 2y ago
Wow, that SO post is full of very bad answers. Regardless ...
Even ignoring compiler extensions, you don't have to memcpy the struct to the network directly (though this necessarily must be doable safely due to the fact that <net/> headers use it). Instead, define an API that takes loose structs, and have generated code do all the swapping and packing from the struct form to the bytes form.
If you're careful, you might make it such that on common platforms this optimizes into just a memcpy. But if the struct can get inlined this doesn't actually matter.