authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2022-12-08 22:05:30+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-08 22:21:46+02:00
log7826e28bd38876e253f5842f53177dd9f45caa9a
treeec1359c39bf25f8377a5f631fb0f529d8c6cc113
parent225ed65ed2cc88fce54660250feaeb44e45943fa

Re-apply: "std.ComptimeStringMap: use tuple types"

096d3efae5fcaa5640f4acb2f9be2d7f93f7fdb2 was not the cause of the CI failure.

2 files changed, 22 insertions(+), 37 deletions(-)

lib/std/comptime_string_map.zig+20-29
......@@ -5,9 +5,8 @@ const mem = std.mem;
55/// Works by separating the keys by length at comptime and only checking strings of
66/// equal length at runtime.
77///
8/// `kvs` expects a list literal containing list literals or an array/slice of structs
9/// where `.@"0"` is the `[]const u8` key and `.@"1"` is the associated value of type `V`.
10/// TODO: https://github.com/ziglang/zig/issues/4335
8/// `kvs_list` expects a list of `struct { []const u8, V }` (key-value pair) tuples.
9/// You can pass `struct { []const u8 }` (only keys) tuples if `V` is `void`.
1110pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type {
1211 const precomputed = comptime blk: {
1312 @setEvalBranchQuota(2000);
......@@ -97,32 +96,26 @@ test "ComptimeStringMap list literal of list literals" {
9796}
9897
9998test "ComptimeStringMap array of structs" {
100 const KV = struct {
101 @"0": []const u8,
102 @"1": TestEnum,
103 };
99 const KV = struct { []const u8, TestEnum };
104100 const map = ComptimeStringMap(TestEnum, [_]KV{
105 .{ .@"0" = "these", .@"1" = .D },
106 .{ .@"0" = "have", .@"1" = .A },
107 .{ .@"0" = "nothing", .@"1" = .B },
108 .{ .@"0" = "incommon", .@"1" = .C },
109 .{ .@"0" = "samelen", .@"1" = .E },
101 .{ "these", .D },
102 .{ "have", .A },
103 .{ "nothing", .B },
104 .{ "incommon", .C },
105 .{ "samelen", .E },
110106 });
111107
112108 try testMap(map);
113109}
114110
115111test "ComptimeStringMap slice of structs" {
116 const KV = struct {
117 @"0": []const u8,
118 @"1": TestEnum,
119 };
112 const KV = struct { []const u8, TestEnum };
120113 const slice: []const KV = &[_]KV{
121 .{ .@"0" = "these", .@"1" = .D },
122 .{ .@"0" = "have", .@"1" = .A },
123 .{ .@"0" = "nothing", .@"1" = .B },
124 .{ .@"0" = "incommon", .@"1" = .C },
125 .{ .@"0" = "samelen", .@"1" = .E },
114 .{ "these", .D },
115 .{ "have", .A },
116 .{ "nothing", .B },
117 .{ "incommon", .C },
118 .{ "samelen", .E },
126119 };
127120 const map = ComptimeStringMap(TestEnum, slice);
128121
......@@ -141,15 +134,13 @@ fn testMap(comptime map: anytype) !void {
141134}
142135
143136test "ComptimeStringMap void value type, slice of structs" {
144 const KV = struct {
145 @"0": []const u8,
146 };
137 const KV = struct { []const u8 };
147138 const slice: []const KV = &[_]KV{
148 .{ .@"0" = "these" },
149 .{ .@"0" = "have" },
150 .{ .@"0" = "nothing" },
151 .{ .@"0" = "incommon" },
152 .{ .@"0" = "samelen" },
139 .{"these"},
140 .{"have"},
141 .{"nothing"},
142 .{"incommon"},
143 .{"samelen"},
153144 };
154145 const map = ComptimeStringMap(void, slice);
155146
lib/std/meta.zig+2-8
......@@ -115,16 +115,10 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
115115 // - https://github.com/ziglang/zig/issues/3863
116116 if (@typeInfo(T).Enum.fields.len <= 100) {
117117 const kvs = comptime build_kvs: {
118 // In order to generate an array of structs that play nice with anonymous
119 // list literals, we need to give them "0" and "1" field names.
120 // TODO https://github.com/ziglang/zig/issues/4335
121 const EnumKV = struct {
122 @"0": []const u8,
123 @"1": T,
124 };
118 const EnumKV = struct { []const u8, T };
125119 var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;
126120 inline for (@typeInfo(T).Enum.fields) |enumField, i| {
127 kvs_array[i] = .{ .@"0" = enumField.name, .@"1" = @field(T, enumField.name) };
121 kvs_array[i] = .{ enumField.name, @field(T, enumField.name) };
128122 }
129123 break :build_kvs kvs_array[0..];
130124 };