| ... | @@ -594,6 +594,47 @@ test "std.meta.FieldEnum" { | ... | @@ -594,6 +594,47 @@ test "std.meta.FieldEnum" { |
| 594 | try expectEqualEnum(enum { a, b, c }, FieldEnum(union { a: u8, b: void, c: f32 })); | 594 | try expectEqualEnum(enum { a, b, c }, FieldEnum(union { a: u8, b: void, c: f32 })); |
| 595 | } | 595 | } |
| 596 | | 596 | |
| | 597 | pub fn DeclEnum(comptime T: type) type { |
| | 598 | const fieldInfos = std.meta.declarations(T); |
| | 599 | var enumDecls: [fieldInfos.len]std.builtin.TypeInfo.EnumField = undefined; |
| | 600 | var decls = [_]std.builtin.TypeInfo.Declaration{}; |
| | 601 | inline for (fieldInfos) |field, i| { |
| | 602 | enumDecls[i] = .{ .name = field.name, .value = i }; |
| | 603 | } |
| | 604 | return @Type(.{ |
| | 605 | .Enum = .{ |
| | 606 | .layout = .Auto, |
| | 607 | .tag_type = std.math.IntFittingRange(0, fieldInfos.len - 1), |
| | 608 | .fields = &enumDecls, |
| | 609 | .decls = &decls, |
| | 610 | .is_exhaustive = true, |
| | 611 | }, |
| | 612 | }); |
| | 613 | } |
| | 614 | |
| | 615 | test "std.meta.DeclEnum" { |
| | 616 | const A = struct { |
| | 617 | const a: u8 = 0; |
| | 618 | }; |
| | 619 | const B = union { |
| | 620 | foo: void, |
| | 621 | |
| | 622 | const a: u8 = 0; |
| | 623 | const b: void = {}; |
| | 624 | const c: f32 = 0; |
| | 625 | }; |
| | 626 | const C = enum { |
| | 627 | bar, |
| | 628 | |
| | 629 | const a: u8 = 0; |
| | 630 | const b: void = {}; |
| | 631 | const c: f32 = 0; |
| | 632 | }; |
| | 633 | try expectEqualEnum(enum { a }, DeclEnum(A)); |
| | 634 | try expectEqualEnum(enum { a, b, c }, DeclEnum(B)); |
| | 635 | try expectEqualEnum(enum { a, b, c }, DeclEnum(C)); |
| | 636 | } |
| | 637 | |
| 597 | pub const TagType = @compileError("deprecated; use Tag"); | 638 | pub const TagType = @compileError("deprecated; use Tag"); |
| 598 | | 639 | |
| 599 | pub fn Tag(comptime T: type) type { | 640 | pub fn Tag(comptime T: type) type { |