mirror of
https://github.com/ziglang/zig.git
synced 2024-11-15 08:33:06 +00:00
Revert "std.ComptimeStringMap: use tuple types"
This reverts commit 096d3efae5
.
This commit is not passing a very important CI test that was recently
added.
This commit is contained in:
parent
d5ecb318c4
commit
225ed65ed2
@ -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);
|
||||
|
||||
|
@ -115,10 +115,16 @@ 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: {
|
||||
const EnumKV = struct { []const u8, T };
|
||||
// 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,
|
||||
};
|
||||
var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;
|
||||
inline for (@typeInfo(T).Enum.fields) |enumField, i| {
|
||||
kvs_array[i] = .{ enumField.name, @field(T, enumField.name) };
|
||||
kvs_array[i] = .{ .@"0" = enumField.name, .@"1" = @field(T, enumField.name) };
|
||||
}
|
||||
break :build_kvs kvs_array[0..];
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user