aboutsummaryrefslogtreecommitdiff
path: root/lib/std/comptime_string_map.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-12-08 02:23:17 -0800
committerAndrew Kelley <andrew@ziglang.org>2022-12-08 02:23:17 -0800
commit225ed65ed2cc88fce54660250feaeb44e45943fa (patch)
tree53912cecb13cf5ddfa10ae19db1917b1f28644a3 /lib/std/comptime_string_map.zig
parentd5ecb318c4550351eb1973d73cac61bc8af2a70f (diff)
downloadzig-225ed65ed2cc88fce54660250feaeb44e45943fa.tar.gz
zig-225ed65ed2cc88fce54660250feaeb44e45943fa.zip
Revert "std.ComptimeStringMap: use tuple types"
This reverts commit 096d3efae5fcaa5640f4acb2f9be2d7f93f7fdb2. This commit is not passing a very important CI test that was recently added.
Diffstat (limited to 'lib/std/comptime_string_map.zig')
-rw-r--r--lib/std/comptime_string_map.zig49
1 files changed, 29 insertions, 20 deletions
diff --git a/lib/std/comptime_string_map.zig b/lib/std/comptime_string_map.zig
index f7736413d5..09ba4e5a3c 100644
--- a/lib/std/comptime_string_map.zig
+++ b/lib/std/comptime_string_map.zig
@@ -5,8 +5,9 @@ const mem = std.mem;
/// Works by separating the keys by length at comptime and only checking strings of
/// equal length at runtime.
///
-/// `kvs_list` expects a list of `struct { []const u8, V }` (key-value pair) tuples.
-/// You can pass `struct { []const u8 }` (only keys) tuples if `V` is `void`.
+/// `kvs` expects a list literal containing list literals or an array/slice of structs
+/// where `.@"0"` is the `[]const u8` key and `.@"1"` is the associated value of type `V`.
+/// TODO: https://github.com/ziglang/zig/issues/4335
pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type {
const precomputed = comptime blk: {
@setEvalBranchQuota(2000);
@@ -96,26 +97,32 @@ test "ComptimeStringMap list literal of list literals" {
}
test "ComptimeStringMap array of structs" {
- const KV = struct { []const u8, TestEnum };
+ const KV = struct {
+ @"0": []const u8,
+ @"1": TestEnum,
+ };
const map = ComptimeStringMap(TestEnum, [_]KV{
- .{ "these", .D },
- .{ "have", .A },
- .{ "nothing", .B },
- .{ "incommon", .C },
- .{ "samelen", .E },
+ .{ .@"0" = "these", .@"1" = .D },
+ .{ .@"0" = "have", .@"1" = .A },
+ .{ .@"0" = "nothing", .@"1" = .B },
+ .{ .@"0" = "incommon", .@"1" = .C },
+ .{ .@"0" = "samelen", .@"1" = .E },
});
try testMap(map);
}
test "ComptimeStringMap slice of structs" {
- const KV = struct { []const u8, TestEnum };
+ const KV = struct {
+ @"0": []const u8,
+ @"1": TestEnum,
+ };
const slice: []const KV = &[_]KV{
- .{ "these", .D },
- .{ "have", .A },
- .{ "nothing", .B },
- .{ "incommon", .C },
- .{ "samelen", .E },
+ .{ .@"0" = "these", .@"1" = .D },
+ .{ .@"0" = "have", .@"1" = .A },
+ .{ .@"0" = "nothing", .@"1" = .B },
+ .{ .@"0" = "incommon", .@"1" = .C },
+ .{ .@"0" = "samelen", .@"1" = .E },
};
const map = ComptimeStringMap(TestEnum, slice);
@@ -134,13 +141,15 @@ fn testMap(comptime map: anytype) !void {
}
test "ComptimeStringMap void value type, slice of structs" {
- const KV = struct { []const u8 };
+ const KV = struct {
+ @"0": []const u8,
+ };
const slice: []const KV = &[_]KV{
- .{"these"},
- .{"have"},
- .{"nothing"},
- .{"incommon"},
- .{"samelen"},
+ .{ .@"0" = "these" },
+ .{ .@"0" = "have" },
+ .{ .@"0" = "nothing" },
+ .{ .@"0" = "incommon" },
+ .{ .@"0" = "samelen" },
};
const map = ComptimeStringMap(void, slice);