authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2021-01-11 10:56:18-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-01-30 13:19:52+02:00
log68ec54f386d387c5dd3f9dc4f0b5e14be6ae10a6
tree85e45ff4284fc836c299b88e0bf2eca61140bd4b
parent290efc074797e893678154efac61f94a38f6bfc1
signature Commit is signed but in an unrecognized format.

std.meta: rename TagType to Tag


2 files changed, 14 insertions(+), 11 deletions(-)

lib/std/json.zig+1-1
...@@ -1138,7 +1138,7 @@ pub const TokenStream = struct {...@@ -1138,7 +1138,7 @@ pub const TokenStream = struct {
1138 }1138 }
1139};1139};
11401140
1141fn checkNext(p: *TokenStream, id: std.meta.TagType(Token)) void {1141fn checkNext(p: *TokenStream, id: std.meta.Tag(Token)) void {
1142 const token = (p.next() catch unreachable).?;1142 const token = (p.next() catch unreachable).?;
1143 debug.assert(std.meta.activeTag(token) == id);1143 debug.assert(std.meta.activeTag(token) == id);
1144}1144}
lib/std/meta.zig+13-10
...@@ -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}
602602
603pub fn TagType(comptime T: type) type {603// Deprecated: use Tag
604pub const TagType = Tag;
605
606pub 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}
610613
611test "std.meta.TagType" {614test "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 };
620623
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}
624627
625///Returns the active tag of a tagged union628///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;
701704
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" {
822825
823pub const IntToEnumError = error{InvalidEnumTag};826pub const IntToEnumError = error{InvalidEnumTag};
824827
825pub fn intToEnum(comptime Tag: type, tag_int: anytype) IntToEnumError!Tag {828pub 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 }