1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const expectSimilarType = extras.expectSimilarType;
5
6pub fn FieldUnion(comptime T: type) type {
7 const fields = std.meta.fields(T);
8 var names: [fields.len][]const u8 = undefined;
9 var types: [fields.len]type = undefined;
10 var attrs: [fields.len]std.builtin.Type.UnionField.Attributes = undefined;
11
12 inline for (fields, 0..) |field, i| {
13 names[i] = field.name;
14 types[i] = field.type;
15 attrs[i] = .{ .@"align" = field.alignment };
16 }
17 return @Union(.auto, std.meta.FieldEnum(T), &names, &types, &attrs);
18}
19
20test {
21 try expectSimilarType(
22 FieldUnion(struct {
23 a: u32,
24 b: u8,
25 c: u16,
26 }),
27 union(enum) {
28 a: u32,
29 b: u8,
30 c: u16,
31 },
32 );
33}