~alva/zig-bare

6653ce81fb30a9bd28140ecebda14cd7dfbe3184 — Alva 4 months ago 7f72ca0
Be more picky about map keys
2 files changed, 20 insertions(+), 1 deletions(-)

M src/bare.zig
M src/test.zig
M src/bare.zig => src/bare.zig +3 -1
@@ 373,7 373,9 @@ fn HashMapType(comptime T: type, field_name: []const u8) type {

fn isValidHashMapKeyType(comptime T: type) bool {
    return switch (@typeInfo(T)) {
        .Int, .Float, .Bool, .Enum, .Array => true,
        .Int, .Float, .Bool, .Enum => true,
        // Strings are allowed, but we don't quite have strings.
        .Pointer => |p| p.size == .Slice,
        else => false,
    };
}

M src/test.zig => src/test.zig +17 -0
@@ 324,6 324,23 @@ test "write map[u8]u8" {
    expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
}

test "write map[string]u8" {
    var buf: [100]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);

    var map = std.HashMap([]const u8, u8, std.hash_map.hashString, std.hash_map.eqlString).init(testing.allocator);

    defer map.deinit();
    _ = try map.put("lol", 2);
    _ = try map.put("zomg", 4);

    try Writer.init().write(map, fbs.outStream());
    // TODO: Fix this; later versions of std.HashMap preserve insertion order.
    const expected = "\x02\x04zomg\x04\x03lol\x02";
    expectEqual(fbs.getWritten().len, 12);
    expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
}

test "write tagged union" {
    var buf: [10]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);