aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-12-08 21:59:43 -0500
committerGitHub <noreply@github.com>2022-12-08 21:59:43 -0500
commit5c67f9ce7a4d9f5aedbe8cfba1f361922701a144 (patch)
tree27572b3b063b780f3b82970c04f94981a017409d /lib/std
parent225ed65ed2cc88fce54660250feaeb44e45943fa (diff)
parentee9fc54cd032b6ac0fcaa422aa9c2826fa370bfa (diff)
downloadzig-5c67f9ce7a4d9f5aedbe8cfba1f361922701a144.tar.gz
zig-5c67f9ce7a4d9f5aedbe8cfba1f361922701a144.zip
Merge pull request #13827 from Vexu/fix-ci
TypedValue: fix handling of tuples represented as empty_struct_value
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/comptime_string_map.zig49
-rw-r--r--lib/std/meta.zig10
2 files changed, 22 insertions, 37 deletions
diff --git a/lib/std/comptime_string_map.zig b/lib/std/comptime_string_map.zig
index 09ba4e5a3c..f7736413d5 100644
--- a/lib/std/comptime_string_map.zig
+++ b/lib/std/comptime_string_map.zig
@@ -5,9 +5,8 @@ const mem = std.mem;
/// Works by separating the keys by length at comptime and only checking strings of
/// equal length at runtime.
///
-/// `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
+/// `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`.
pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type {
const precomputed = comptime blk: {
@setEvalBranchQuota(2000);
@@ -97,32 +96,26 @@ test "ComptimeStringMap list literal of list literals" {
}
test "ComptimeStringMap array of structs" {
- const KV = struct {
- @"0": []const u8,
- @"1": TestEnum,
- };
+ const KV = struct { []const u8, TestEnum };
const map = ComptimeStringMap(TestEnum, [_]KV{
- .{ .@"0" = "these", .@"1" = .D },
- .{ .@"0" = "have", .@"1" = .A },
- .{ .@"0" = "nothing", .@"1" = .B },
- .{ .@"0" = "incommon", .@"1" = .C },
- .{ .@"0" = "samelen", .@"1" = .E },
+ .{ "these", .D },
+ .{ "have", .A },
+ .{ "nothing", .B },
+ .{ "incommon", .C },
+ .{ "samelen", .E },
});
try testMap(map);
}
test "ComptimeStringMap slice of structs" {
- const KV = struct {
- @"0": []const u8,
- @"1": TestEnum,
- };
+ const KV = struct { []const u8, TestEnum };
const slice: []const KV = &[_]KV{
- .{ .@"0" = "these", .@"1" = .D },
- .{ .@"0" = "have", .@"1" = .A },
- .{ .@"0" = "nothing", .@"1" = .B },
- .{ .@"0" = "incommon", .@"1" = .C },
- .{ .@"0" = "samelen", .@"1" = .E },
+ .{ "these", .D },
+ .{ "have", .A },
+ .{ "nothing", .B },
+ .{ "incommon", .C },
+ .{ "samelen", .E },
};
const map = ComptimeStringMap(TestEnum, slice);
@@ -141,15 +134,13 @@ fn testMap(comptime map: anytype) !void {
}
test "ComptimeStringMap void value type, slice of structs" {
- const KV = struct {
- @"0": []const u8,
- };
+ const KV = struct { []const u8 };
const slice: []const KV = &[_]KV{
- .{ .@"0" = "these" },
- .{ .@"0" = "have" },
- .{ .@"0" = "nothing" },
- .{ .@"0" = "incommon" },
- .{ .@"0" = "samelen" },
+ .{"these"},
+ .{"have"},
+ .{"nothing"},
+ .{"incommon"},
+ .{"samelen"},
};
const map = ComptimeStringMap(void, slice);
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index b2fd10107b..9dd1a97baf 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -115,16 +115,10 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
// - https://github.com/ziglang/zig/issues/3863
if (@typeInfo(T).Enum.fields.len <= 100) {
const kvs = comptime build_kvs: {
- // In order to generate an array of structs that play nice with anonymous
- // list literals, we need to give them "0" and "1" field names.
- // TODO https://github.com/ziglang/zig/issues/4335
- const EnumKV = struct {
- @"0": []const u8,
- @"1": T,
- };
+ const EnumKV = struct { []const u8, T };
var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;
inline for (@typeInfo(T).Enum.fields) |enumField, i| {
- kvs_array[i] = .{ .@"0" = enumField.name, .@"1" = @field(T, enumField.name) };
+ kvs_array[i] = .{ enumField.name, @field(T, enumField.name) };
}
break :build_kvs kvs_array[0..];
};