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