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