-
Book Overview & Buying
-
Table Of Contents
Systems Programming with Zig
By :
Before examining the code, it is worth grounding the comptime parser in concrete terms. Consider what happens with a hand-written parser when the MemQueryResp struct gains a new field — say, a swap_mb value to report swap usage. With a hand-written approach, you must remember to update the encoder, update the decoder, update any size constants, and update any documentation that describes the wire format. Miss any one of those, and the parser silently reads the wrong value into the wrong field with no error until a live message produces nonsense output. With the comptime parser, adding the field to the struct is the only change required. The encoder and decoder are regenerated automatically on the next build because they are derived from @typeInfo(MemQueryResp) at compile time. There is no separate parser file to synchronize. A second scenario involves field types. Suppose a developer tries to add a f64 field to store a CPU utilization...