Something went wrong. Try again.
A SpaceTraders Agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172pub fn CombinedEnum(comptime parent: type) type { const parent_info = @typeInfo(parent).@"struct";
comptime var num_fields = 0; comptime var num_enums = 0;
inline for (parent_info.decls) |partial| { switch (@typeInfo(@field(parent, partial.name))) { .@"enum" => |e| { num_fields += e.fields.len; num_enums += 1; }, else => {}, } }
const ConversionMap = struct { start: comptime_int = 0, end: comptime_int = 0, T: type, };
comptime var combined: [num_fields][:0]const u8 = undefined; comptime var combined_values: [num_fields]u8 = undefined; comptime var conv_map: [num_enums]ConversionMap = undefined;
comptime var idx = 0; comptime var i = 0;
inline for (parent_info.decls) |partial| { switch (@typeInfo(@field(parent, partial.name))) { .@"enum" => |e| { conv_map[idx] = .{ .start = i, .end = i + e.fields.len - 1, .T = @field(parent, partial.name), };
for (e.fields) |field| { combined[i] = field.name; combined_values[i] = field.value; i += 1; }
idx += 1; }, else => {}, } }
const combined_enum = @Enum( u8, .nonexhaustive, &combined, &combined_values, );
return struct { pub const Combined = combined_enum;
const conversion_map: [num_enums]ConversionMap = conv_map;
pub fn originalType(value: @TypeOf(Combined)) type { const int_rep: u8 = @intFromEnum(value);
for (conversion_map) |map| { if (map.start <= int_rep and int_rep <= map.end) return map.T; } } };}