| ... | ... | @@ -600,15 +600,18 @@ test "std.meta.FieldEnum" { |
| 600 | 600 | expectEqualEnum(enum { a, b, c }, FieldEnum(union { a: u8, b: void, c: f32 })); |
| 601 | 601 | } |
| 602 | 602 | |
| 603 | | pub fn TagType(comptime T: type) type { |
| 603 | // Deprecated: use Tag |
| 604 | pub const TagType = Tag; |
| 605 | |
| 606 | pub fn Tag(comptime T: type) type { |
| 604 | 607 | return switch (@typeInfo(T)) { |
| 605 | 608 | .Enum => |info| info.tag_type, |
| 606 | | .Union => |info| if (info.tag_type) |Tag| Tag else null, |
| 609 | .Union => |info| if (info.tag_type) |TheTag| TheTag else null, |
| 607 | 610 | else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"), |
| 608 | 611 | }; |
| 609 | 612 | } |
| 610 | 613 | |
| 611 | | test "std.meta.TagType" { |
| 614 | test "std.meta.Tag" { |
| 612 | 615 | const E = enum(u8) { |
| 613 | 616 | C = 33, |
| 614 | 617 | D, |
| ... | ... | @@ -618,8 +621,8 @@ test "std.meta.TagType" { |
| 618 | 621 | D: u16, |
| 619 | 622 | }; |
| 620 | 623 | |
| 621 | | testing.expect(TagType(E) == u8); |
| 622 | | testing.expect(TagType(U) == E); |
| 624 | testing.expect(Tag(E) == u8); |
| 625 | testing.expect(Tag(U) == E); |
| 623 | 626 | } |
| 624 | 627 | |
| 625 | 628 | ///Returns the active tag of a tagged union |
| ... | ... | @@ -694,13 +697,13 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool { |
| 694 | 697 | } |
| 695 | 698 | }, |
| 696 | 699 | .Union => |info| { |
| 697 | | if (info.tag_type) |Tag| { |
| 700 | if (info.tag_type) |UnionTag| { |
| 698 | 701 | const tag_a = activeTag(a); |
| 699 | 702 | const tag_b = activeTag(b); |
| 700 | 703 | if (tag_a != tag_b) return false; |
| 701 | 704 | |
| 702 | 705 | inline for (info.fields) |field_info| { |
| 703 | | if (@field(Tag, field_info.name) == tag_a) { |
| 706 | if (@field(UnionTag, field_info.name) == tag_a) { |
| 704 | 707 | return eql(@field(a, field_info.name), @field(b, field_info.name)); |
| 705 | 708 | } |
| 706 | 709 | } |
| ... | ... | @@ -822,9 +825,9 @@ test "intToEnum with error return" { |
| 822 | 825 | |
| 823 | 826 | pub const IntToEnumError = error{InvalidEnumTag}; |
| 824 | 827 | |
| 825 | | pub fn intToEnum(comptime Tag: type, tag_int: anytype) IntToEnumError!Tag { |
| 826 | | inline for (@typeInfo(Tag).Enum.fields) |f| { |
| 827 | | const this_tag_value = @field(Tag, f.name); |
| 828 | pub fn intToEnum(comptime EnumTag: type, tag_int: anytype) IntToEnumError!EnumTag { |
| 829 | inline for (@typeInfo(EnumTag).Enum.fields) |f| { |
| 830 | const this_tag_value = @field(EnumTag, f.name); |
| 828 | 831 | if (tag_int == @enumToInt(this_tag_value)) { |
| 829 | 832 | return this_tag_value; |
| 830 | 833 | } |