Macro for binary data deserialization into tuples of variable types

For the generated function, you’d have to do it like this:

@generated function read_schema_gen2(io::IOBuffer, types::Type...)
    reads = (:(read(io, $(t.parameters[1]))) for t in types)
    return :(tuple($(reads...)))
end

types is a tuple of Type a the time the generation happens, so you have to get out the parameters.

I think it won’t be getting better than that:

julia> @code_lowered read_schema_gen2(IOBuffer(bytevec), types...)
CodeInfo(
    @ REPL[38]:2 within `read_schema_gen2'
   ┌ @ REPL[38] within `macro expansion'
1 ─│ %1 = Main.read(io, Int8)
│  │ %2 = Main.read(io, Int16)
│  │ %3 = Main.read(io, Int32)
│  │ %4 = Main.tuple(%1, %2, %3)
└──│      return %4
   └
)
1 Like