authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-07-09 13:42:05 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-07-09 13:42:05 -07:00
logcfeeff77833f0e180e116859b2c7370a7c38d8f0
treefaf5c9775e3fa2d76d749639ab55ef63b05dcb86
parenta16a0441443899b8ebc14a6693b5262512c65168

add FieldUnion


1 files changed, 19 insertions(+), 0 deletions(-)

src/lib.zig+19
...@@ -542,3 +542,22 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 {...@@ -542,3 +542,22 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 {
542 std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&array)}) catch unreachable;542 std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&array)}) catch unreachable;
543 return res;543 return res;
544}544}
545
546pub fn FieldUnion(comptime T: type) type {
547 const infos = std.meta.fields(T);
548
549 var fields: [infos.len]std.builtin.Type.UnionField = undefined;
550 inline for (infos, 0..) |field, i| {
551 fields[i] = .{
552 .name = field.name,
553 .type = field.type,
554 .alignment = field.alignment,
555 };
556 }
557 return @Type(std.builtin.Type{ .Union = .{
558 .layout = .Auto,
559 .tag_type = std.meta.FieldEnum(T),
560 .fields = &fields,
561 .decls = &.{},
562 } });
563}