| ... | ... | @@ -568,6 +568,43 @@ test "std.meta.fieldNames" { |
| 568 | 568 | testing.expectEqualSlices(u8, u1names[1], "b"); |
| 569 | 569 | } |
| 570 | 570 | |
| 571 | pub fn FieldEnum(comptime T: type) type { |
| 572 | const fieldInfos = fields(T); |
| 573 | var enumFields: [fieldInfos.len]std.builtin.TypeInfo.EnumField = undefined; |
| 574 | var decls = [_]std.builtin.TypeInfo.Declaration{}; |
| 575 | inline for (fieldInfos) |field, i| { |
| 576 | enumFields[i] = .{ |
| 577 | .name = field.name, |
| 578 | .value = i, |
| 579 | }; |
| 580 | } |
| 581 | return @Type(.{ |
| 582 | .Enum = .{ |
| 583 | .layout = .Auto, |
| 584 | .tag_type = std.math.IntFittingRange(0, fieldInfos.len - 1), |
| 585 | .fields = &enumFields, |
| 586 | .decls = &decls, |
| 587 | .is_exhaustive = true, |
| 588 | }, |
| 589 | }); |
| 590 | } |
| 591 | |
| 592 | fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) void { |
| 593 | // TODO: https://github.com/ziglang/zig/issues/7419 |
| 594 | // testing.expectEqual(@typeInfo(expected).Enum, @typeInfo(actual).Enum); |
| 595 | testing.expectEqual(@typeInfo(expected).Enum.layout, @typeInfo(actual).Enum.layout); |
| 596 | testing.expectEqual(@typeInfo(expected).Enum.tag_type, @typeInfo(actual).Enum.tag_type); |
| 597 | comptime testing.expectEqualSlices(std.builtin.TypeInfo.EnumField, @typeInfo(expected).Enum.fields, @typeInfo(actual).Enum.fields); |
| 598 | comptime testing.expectEqualSlices(std.builtin.TypeInfo.Declaration, @typeInfo(expected).Enum.decls, @typeInfo(actual).Enum.decls); |
| 599 | testing.expectEqual(@typeInfo(expected).Enum.is_exhaustive, @typeInfo(actual).Enum.is_exhaustive); |
| 600 | } |
| 601 | |
| 602 | test "std.meta.FieldEnum" { |
| 603 | expectEqualEnum(enum { a }, FieldEnum(struct { a: u8 })); |
| 604 | expectEqualEnum(enum { a, b, c }, FieldEnum(struct { a: u8, b: void, c: f32 })); |
| 605 | expectEqualEnum(enum { a, b, c }, FieldEnum(union { a: u8, b: void, c: f32 })); |
| 606 | } |
| 607 | |
| 571 | 608 | pub fn TagType(comptime T: type) type { |
| 572 | 609 | return switch (@typeInfo(T)) { |
| 573 | 610 | .Enum => |info| info.tag_type, |