authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-07-10 11:35:36-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-10 14:35:36-04:00
log3d5751b57922a2ccadde70b63fac91665a74cae7
tree27c7f644e992c691c1abd70ab7edf092c587697f
parentcd0594e4a6a8449e3c1c9da5130a4252869e627c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.meta: remove isTag (#15584)

This is not used by Zig itself anywhere and not using the function is more idiomatic.

1 files changed, 1 insertions(+), 47 deletions(-)

lib/std/meta.zig+1-47
......@@ -16,53 +16,7 @@ test {
1616
1717pub const tagName = @compileError("deprecated; use @tagName or @errorName directly");
1818
19/// Given an enum or tagged union, returns true if the comptime-supplied
20/// string matches the name of the tag value. This match process should
21/// be, at runtime, O(1) in the number of tags available to the enum or
22/// union, and it should also be O(1) in the length of the comptime tag
23/// names.
24pub fn isTag(tagged_value: anytype, comptime tag_name: []const u8) bool {
25 const T = @TypeOf(tagged_value);
26 const type_info = @typeInfo(T);
27 const type_name = @typeName(T);
28
29 // select the Enum type out of the type (in the case of the tagged union, extract it)
30 const E = if (.Enum == type_info) T else if (.Union == type_info) (type_info.Union.tag_type orelse {
31 @compileError("attempted to use isTag on the untagged union " ++ type_name);
32 }) else {
33 @compileError("attempted to use isTag on a value of type (" ++ type_name ++ ") that isn't an enum or a union.");
34 };
35
36 return tagged_value == @field(E, tag_name);
37}
38
39test "std.meta.isTag for Enums" {
40 const EnumType = enum { a, b };
41 var a_type: EnumType = .a;
42 var b_type: EnumType = .b;
43
44 try testing.expect(isTag(a_type, "a"));
45 try testing.expect(!isTag(a_type, "b"));
46 try testing.expect(isTag(b_type, "b"));
47 try testing.expect(!isTag(b_type, "a"));
48}
49
50test "std.meta.isTag for Tagged Unions" {
51 const TaggedUnionEnum = enum { int, flt };
52
53 const TaggedUnionType = union(TaggedUnionEnum) {
54 int: i64,
55 flt: f64,
56 };
57
58 var int = TaggedUnionType{ .int = 1234 };
59 var flt = TaggedUnionType{ .flt = 12.34 };
60
61 try testing.expect(isTag(int, "int"));
62 try testing.expect(!isTag(int, "flt"));
63 try testing.expect(isTag(flt, "flt"));
64 try testing.expect(!isTag(flt, "int"));
65}
19pub const isTag = @compileError("deprecated; use 'tagged_value == @field(E, tag_name)' directly");
6620
6721/// Returns the variant of an enum type, `T`, which is named `str`, or `null` if no such variant exists.
6822pub fn stringToEnum(comptime T: type, str: []const u8) ?T {