| ... | @@ -5,9 +5,8 @@ const mem = std.mem; | ... | @@ -5,9 +5,8 @@ const mem = std.mem; |
| 5 | /// Works by separating the keys by length at comptime and only checking strings of | 5 | /// Works by separating the keys by length at comptime and only checking strings of |
| 6 | /// equal length at runtime. | 6 | /// equal length at runtime. |
| 7 | /// | 7 | /// |
| 8 | /// `kvs` expects a list literal containing list literals or an array/slice of structs | 8 | /// `kvs_list` expects a list of `struct { []const u8, V }` (key-value pair) tuples. |
| 9 | /// where `.@"0"` is the `[]const u8` key and `.@"1"` is the associated value of type `V`. | 9 | /// You can pass `struct { []const u8 }` (only keys) tuples if `V` is `void`. |
| 10 | /// TODO: https://github.com/ziglang/zig/issues/4335 | | |
| 11 | pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type { | 10 | pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type { |
| 12 | const precomputed = comptime blk: { | 11 | const precomputed = comptime blk: { |
| 13 | @setEvalBranchQuota(2000); | 12 | @setEvalBranchQuota(2000); |
| ... | @@ -97,32 +96,26 @@ test "ComptimeStringMap list literal of list literals" { | ... | @@ -97,32 +96,26 @@ test "ComptimeStringMap list literal of list literals" { |
| 97 | } | 96 | } |
| 98 | | 97 | |
| 99 | test "ComptimeStringMap array of structs" { | 98 | test "ComptimeStringMap array of structs" { |
| 100 | const KV = struct { | 99 | const KV = struct { []const u8, TestEnum }; |
| 101 | @"0": []const u8, | | |
| 102 | @"1": TestEnum, | | |
| 103 | }; | | |
| 104 | const map = ComptimeStringMap(TestEnum, [_]KV{ | 100 | const map = ComptimeStringMap(TestEnum, [_]KV{ |
| 105 | .{ .@"0" = "these", .@"1" = .D }, | 101 | .{ "these", .D }, |
| 106 | .{ .@"0" = "have", .@"1" = .A }, | 102 | .{ "have", .A }, |
| 107 | .{ .@"0" = "nothing", .@"1" = .B }, | 103 | .{ "nothing", .B }, |
| 108 | .{ .@"0" = "incommon", .@"1" = .C }, | 104 | .{ "incommon", .C }, |
| 109 | .{ .@"0" = "samelen", .@"1" = .E }, | 105 | .{ "samelen", .E }, |
| 110 | }); | 106 | }); |
| 111 | | 107 | |
| 112 | try testMap(map); | 108 | try testMap(map); |
| 113 | } | 109 | } |
| 114 | | 110 | |
| 115 | test "ComptimeStringMap slice of structs" { | 111 | test "ComptimeStringMap slice of structs" { |
| 116 | const KV = struct { | 112 | const KV = struct { []const u8, TestEnum }; |
| 117 | @"0": []const u8, | | |
| 118 | @"1": TestEnum, | | |
| 119 | }; | | |
| 120 | const slice: []const KV = &[_]KV{ | 113 | const slice: []const KV = &[_]KV{ |
| 121 | .{ .@"0" = "these", .@"1" = .D }, | 114 | .{ "these", .D }, |
| 122 | .{ .@"0" = "have", .@"1" = .A }, | 115 | .{ "have", .A }, |
| 123 | .{ .@"0" = "nothing", .@"1" = .B }, | 116 | .{ "nothing", .B }, |
| 124 | .{ .@"0" = "incommon", .@"1" = .C }, | 117 | .{ "incommon", .C }, |
| 125 | .{ .@"0" = "samelen", .@"1" = .E }, | 118 | .{ "samelen", .E }, |
| 126 | }; | 119 | }; |
| 127 | const map = ComptimeStringMap(TestEnum, slice); | 120 | const map = ComptimeStringMap(TestEnum, slice); |
| 128 | | 121 | |
| ... | @@ -141,15 +134,13 @@ fn testMap(comptime map: anytype) !void { | ... | @@ -141,15 +134,13 @@ fn testMap(comptime map: anytype) !void { |
| 141 | } | 134 | } |
| 142 | | 135 | |
| 143 | test "ComptimeStringMap void value type, slice of structs" { | 136 | test "ComptimeStringMap void value type, slice of structs" { |
| 144 | const KV = struct { | 137 | const KV = struct { []const u8 }; |
| 145 | @"0": []const u8, | | |
| 146 | }; | | |
| 147 | const slice: []const KV = &[_]KV{ | 138 | const slice: []const KV = &[_]KV{ |
| 148 | .{ .@"0" = "these" }, | 139 | .{"these"}, |
| 149 | .{ .@"0" = "have" }, | 140 | .{"have"}, |
| 150 | .{ .@"0" = "nothing" }, | 141 | .{"nothing"}, |
| 151 | .{ .@"0" = "incommon" }, | 142 | .{"incommon"}, |
| 152 | .{ .@"0" = "samelen" }, | 143 | .{"samelen"}, |
| 153 | }; | 144 | }; |
| 154 | const map = ComptimeStringMap(void, slice); | 145 | const map = ComptimeStringMap(void, slice); |
| 155 | | 146 | |