aboutsummaryrefslogtreecommitdiff
path: root/lib/std/meta.zig
diff options
context:
space:
mode:
authorJimmi Holst Christensen <jhc@dismail.de>2022-04-25 19:08:39 +0200
committerAndrew Kelley <andrew@ziglang.org>2022-04-26 17:52:21 -0400
commitcea310c9082a985b103953dd84579aa1a6dce6ea (patch)
treeca107880795aca1ed9be4fe0f4d6e4e929a4e9c4 /lib/std/meta.zig
parent18f30346291bd2471e07924af161de080935dd60 (diff)
downloadzig-cea310c9082a985b103953dd84579aa1a6dce6ea.tar.gz
zig-cea310c9082a985b103953dd84579aa1a6dce6ea.zip
Remove usage of inline for from print_targets.cmdTargets
This function was one of the biggest zig functions in a debug build of the compiler. $ bloaty stage3-debug/bin/zig -d symbols --tsv -n 10000000 | rg -v '(llvm|clang|std|lld|\(anonymous namespace\))::|\[section ' | sort -h -k 3 ... translate_c.ast.renderNode 86168 86219 main.buildOutputType 177959 178004 InfoTable 184832 184870 AArch64SVEIntrinsicMap 188544 188596 print_targets.cmdTargets__anon_4735 319156 319216 __static_initialization_and_destruction_0() 486666 489582 MatchTable1 621884 621997 OperandMatchTable 1139622 1139861 MatchTable0 1899764 1900141
Diffstat (limited to 'lib/std/meta.zig')
-rw-r--r--lib/std/meta.zig27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 33f690f7aa..9940fafba0 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -568,6 +568,33 @@ test "std.meta.fieldNames" {
try testing.expectEqualSlices(u8, u1names[1], "b");
}
+/// Given an enum or error set type, returns a pointer to an array containing all tags for that
+/// enum or error set.
+pub fn tags(comptime T: type) *const [fields(T).len]T {
+ comptime {
+ const fieldInfos = fields(T);
+ var res: [fieldInfos.len]T = undefined;
+ for (fieldInfos) |field, i| {
+ res[i] = @field(T, field.name);
+ }
+ return &res;
+ }
+}
+
+test "std.meta.tags" {
+ const E1 = enum { A, B };
+ const E2 = error{A};
+
+ const e1_tags = tags(E1);
+ const e2_tags = tags(E2);
+
+ try testing.expect(e1_tags.len == 2);
+ try testing.expectEqual(E1.A, e1_tags[0]);
+ try testing.expectEqual(E1.B, e1_tags[1]);
+ try testing.expect(e2_tags.len == 1);
+ try testing.expectEqual(E2.A, e2_tags[0]);
+}
+
pub fn FieldEnum(comptime T: type) type {
const field_infos = fields(T);
var enumFields: [field_infos.len]std.builtin.Type.EnumField = undefined;