| ... | @@ -22,7 +22,11 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs: var) type { | ... | @@ -22,7 +22,11 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs: var) type { |
| 22 | } | 22 | } |
| 23 | }).lenAsc; | 23 | }).lenAsc; |
| 24 | for (kvs) |kv, i| { | 24 | for (kvs) |kv, i| { |
| 25 | sorted_kvs[i] = .{.key = kv.@"0", .value = kv.@"1"}; | 25 | if (V != void) { |
| | 26 | sorted_kvs[i] = .{.key = kv.@"0", .value = kv.@"1"}; |
| | 27 | } else { |
| | 28 | sorted_kvs[i] = .{.key = kv.@"0", .value = {}}; |
| | 29 | } |
| 26 | } | 30 | } |
| 27 | std.sort.sort(KV, &sorted_kvs, lenAsc); | 31 | std.sort.sort(KV, &sorted_kvs, lenAsc); |
| 28 | const min_len = sorted_kvs[0].key.len; | 32 | const min_len = sorted_kvs[0].key.len; |
| ... | @@ -132,3 +136,42 @@ fn testMap(comptime map: var) void { | ... | @@ -132,3 +136,42 @@ fn testMap(comptime map: var) void { |
| 132 | std.testing.expect(!map.has("missing")); | 136 | std.testing.expect(!map.has("missing")); |
| 133 | std.testing.expect(map.has("these")); | 137 | std.testing.expect(map.has("these")); |
| 134 | } | 138 | } |
| | 139 | |
| | 140 | test "ComptimeStringMap void value type, slice of structs" { |
| | 141 | const KV = struct { |
| | 142 | @"0": []const u8, |
| | 143 | }; |
| | 144 | const slice: []const KV = &[_]KV{ |
| | 145 | .{.@"0" = "these"}, |
| | 146 | .{.@"0" = "have"}, |
| | 147 | .{.@"0" = "nothing"}, |
| | 148 | .{.@"0" = "incommon"}, |
| | 149 | .{.@"0" = "samelen"}, |
| | 150 | }; |
| | 151 | const map = ComptimeStringMap(void, slice); |
| | 152 | |
| | 153 | testSet(map); |
| | 154 | } |
| | 155 | |
| | 156 | test "ComptimeStringMap void value type, list literal of list literals" { |
| | 157 | const map = ComptimeStringMap(void, .{ |
| | 158 | .{"these"}, |
| | 159 | .{"have"}, |
| | 160 | .{"nothing"}, |
| | 161 | .{"incommon"}, |
| | 162 | .{"samelen"}, |
| | 163 | }); |
| | 164 | |
| | 165 | testSet(map); |
| | 166 | } |
| | 167 | |
| | 168 | fn testSet(comptime map: var) void { |
| | 169 | std.testing.expectEqual({}, map.get("have").?); |
| | 170 | std.testing.expectEqual({}, map.get("nothing").?); |
| | 171 | std.testing.expect(null == map.get("missing")); |
| | 172 | std.testing.expectEqual({}, map.get("these").?); |
| | 173 | std.testing.expectEqual({}, map.get("samelen").?); |
| | 174 | |
| | 175 | std.testing.expect(!map.has("missing")); |
| | 176 | std.testing.expect(map.has("these")); |
| | 177 | } |