Skip to content

Commit 225ed65

Browse files
committed
Revert "std.ComptimeStringMap: use tuple types"
This reverts commit 096d3ef. This commit is not passing a very important CI test that was recently added.
1 parent d5ecb31 commit 225ed65

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

lib/std/comptime_string_map.zig

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ 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_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`.
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
1011
pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type {
1112
const precomputed = comptime blk: {
1213
@setEvalBranchQuota(2000);
@@ -96,26 +97,32 @@ test "ComptimeStringMap list literal of list literals" {
9697
}
9798

9899
test "ComptimeStringMap array of structs" {
99-
const KV = struct { []const u8, TestEnum };
100+
const KV = struct {
101+
@"0": []const u8,
102+
@"1": TestEnum,
103+
};
100104
const map = ComptimeStringMap(TestEnum, [_]KV{
101-
.{ "these", .D },
102-
.{ "have", .A },
103-
.{ "nothing", .B },
104-
.{ "incommon", .C },
105-
.{ "samelen", .E },
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 },
106110
});
107111

108112
try testMap(map);
109113
}
110114

111115
test "ComptimeStringMap slice of structs" {
112-
const KV = struct { []const u8, TestEnum };
116+
const KV = struct {
117+
@"0": []const u8,
118+
@"1": TestEnum,
119+
};
113120
const slice: []const KV = &[_]KV{
114-
.{ "these", .D },
115-
.{ "have", .A },
116-
.{ "nothing", .B },
117-
.{ "incommon", .C },
118-
.{ "samelen", .E },
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 },
119126
};
120127
const map = ComptimeStringMap(TestEnum, slice);
121128

@@ -134,13 +141,15 @@ fn testMap(comptime map: anytype) !void {
134141
}
135142

136143
test "ComptimeStringMap void value type, slice of structs" {
137-
const KV = struct { []const u8 };
144+
const KV = struct {
145+
@"0": []const u8,
146+
};
138147
const slice: []const KV = &[_]KV{
139-
.{"these"},
140-
.{"have"},
141-
.{"nothing"},
142-
.{"incommon"},
143-
.{"samelen"},
148+
.{ .@"0" = "these" },
149+
.{ .@"0" = "have" },
150+
.{ .@"0" = "nothing" },
151+
.{ .@"0" = "incommon" },
152+
.{ .@"0" = "samelen" },
144153
};
145154
const map = ComptimeStringMap(void, slice);
146155

lib/std/meta.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,16 @@ 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-
const EnumKV = struct { []const u8, T };
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+
};
119125
var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;
120126
inline for (@typeInfo(T).Enum.fields) |enumField, i| {
121-
kvs_array[i] = .{ enumField.name, @field(T, enumField.name) };
127+
kvs_array[i] = .{ .@"0" = enumField.name, .@"1" = @field(T, enumField.name) };
122128
}
123129
break :build_kvs kvs_array[0..];
124130
};

0 commit comments

Comments
 (0)