In short, we have a tagged union, Control, that can store a pointer to one of the Controls types listed. Because it’s a tagged union, the variable itself stores both the pointer as well as a “tag” value indicating which of the variants is being stored, so there’s no type confusion.
(One of these is unfortunately still 16 bytes on 64-bit systems: the tag, which could theoretically be stored in 3 bits, nonetheless has a full 64 bits allocated to it, because the payload that follows is a pointer, which must be aligned. ¯\_(ツ)_/¯)
Now, these Controls types all carry a generation: usize member. One way of getting the generation of an arbitrary Control would be this:
This works fine, and is probably optimal. But what’s even more optimal is letting comptime do the codegen for you. Returning to the definition above, we find:
inline else is inline in the same way inline for and inline while are. For every unhandled case, a prong is unrolled. This means the body must compile for each possible capture (i.e. each payload type). (You can of course do comptime calls here, to do different things with different kinds of payloads, though please consider your complexity budget!)