Returns an enum with a variant named after each field of T.
pub fn FieldEnum(comptime T: type) type
pub fn FieldEnum(comptime T: type) type {
const field_names = fieldNames(T);
switch (@typeInfo(T)) {
.@"union" => |@"union"| if (@"union".tag_type) |EnumTag| {
for (std.enums.values(EnumTag), 0..) |v, i| {
if (@backingInt(v) != i) break; // enum values not consecutive
if (!std.mem.eql(u8, @tagName(v), field_names[i])) break; // fields out of order
} else {
return EnumTag;
}
},
else => {},
}
if (field_names.len == 0) return enum {};
const IntTag = std.math.IntFittingRange(0, field_names.len - 1);
return @Enum(IntTag, .exhaustive, field_names, &std.simd.iota(IntTag, field_names.len));
}