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