~alva/zig-bare

5458d9c638814b71d3b76186832e9f887bae1e09 — ugla 7 days ago 9f00dc5 trunk 0.6.0
Update to Zig 0.8.0
3 files changed, 90 insertions(+), 97 deletions(-)

M README.md
M src/bare.zig
M src/test.zig
M README.md => README.md +1 -1
@@ 4,7 4,7 @@

## Building

Tested on Zig 0.7.1.
Tested on Zig 0.8.0.

```shell
# Use -Drelease-{fast, safe, small} for release build.

M src/bare.zig => src/bare.zig +18 -25
@@ 180,7 180,7 @@ pub const Decoder = struct {
            @compileError("unsupported hashmap key type " ++ @typeName(K));

        var i = try self.decodeVarUint(reader);
        var map = T.Unmanaged.init(&self.arena.allocator);
        var map = T.Unmanaged{};

        if (i != 0) {
            if (std.math.maxInt(T.Size) < i)


@@ 328,18 328,11 @@ pub const Encoder = struct {

        try self.encodeVarUint(value.count(), writer);

        if (@hasDecl(T, "items")) {
            for (value.items()) |entry| {
                try self.encode(entry.key, writer);
                try self.encode(entry.value, writer);
            }
        } else {
            var it = value.iterator();
        var it = value.iterator();

            while (it.next()) |kv| {
                try self.encode(kv.key, writer);
                try self.encode(kv.value, writer);
            }
        while (it.next()) |kv| {
            try self.encode(kv.key_ptr.*, writer);
            try self.encode(kv.value_ptr.*, writer);
        }
    }



@@ 381,7 374,7 @@ fn HashMapValueType(comptime T: type) type {
}

fn HashMapType(comptime T: type, comptime field_name: []const u8) type {
    const fields = @typeInfo(T.Entry).Struct.fields;
    const fields = @typeInfo(T.KV).Struct.fields;

    inline for (fields) |f|
        if (comptime mem.eql(u8, f.name, field_name))


@@ 399,38 392,38 @@ fn isValidHashMapKeyType(comptime T: type) bool {

test "read variable uint" {
    const x = try Decoder.init(std.testing.allocator).decodeVarUint(io.fixedBufferStream("\x2a").reader());
    std.testing.expectEqual(x, 42);
    try std.testing.expectEqual(x, 42);
    const y = try Decoder.init(std.testing.allocator).decodeVarUint(io.fixedBufferStream("\x80\x02").reader());
    std.testing.expectEqual(y, 0x100);
    try std.testing.expectEqual(y, 0x100);
}

test "read variable uint overflow 1" {
    const buf = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02";
    const x = Decoder.init(std.testing.allocator).decodeVarUint(io.fixedBufferStream(buf).reader());
    std.testing.expectError(DecodeError.Overflow, x);
    try std.testing.expectError(DecodeError.Overflow, x);
}

test "read variable uint overflow 2" {
    const buf = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00";
    const x = Decoder.init(std.testing.allocator).decodeVarUint(io.fixedBufferStream(buf).reader());
    std.testing.expectError(DecodeError.Overflow, x);
    try std.testing.expectError(DecodeError.Overflow, x);
}

test "write variable uint" {
    var buf: [4]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encodeVarUint(42, fbs.writer());
    std.testing.expectEqual(fbs.getWritten()[0], 42);
    try std.testing.expectEqual(fbs.getWritten()[0], 42);
    try Encoder.init().encodeVarUint(0x100, fbs.writer());
    std.testing.expectEqual(fbs.getWritten()[1], 128);
    std.testing.expectEqual(fbs.getWritten()[2], 2);
    try std.testing.expectEqual(fbs.getWritten()[1], 128);
    try std.testing.expectEqual(fbs.getWritten()[2], 2);
}

test "write variable int" {
    var buf: [4]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encodeVarInt(42, fbs.writer());
    std.testing.expectEqual(fbs.getWritten()[0], 42 << 1);
    try std.testing.expectEqual(fbs.getWritten()[0], 42 << 1);
}

test "round trip variable uint" {


@@ 438,7 431,7 @@ test "round trip variable uint" {
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encodeVarUint(0x10000, fbs.writer());
    const res = try Decoder.init(std.testing.allocator).decodeVarUint(io.fixedBufferStream(fbs.getWritten()).reader());
    std.testing.expectEqual(res, 0x10000);
    try std.testing.expectEqual(res, 0x10000);
}

test "round trip variable int 1" {


@@ 446,7 439,7 @@ test "round trip variable int 1" {
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encodeVarInt(-0x10000, fbs.writer());
    const res = try Decoder.init(std.testing.allocator).decodeVarInt(io.fixedBufferStream(fbs.getWritten()).reader());
    std.testing.expectEqual(res, -0x10000);
    try std.testing.expectEqual(res, -0x10000);
}

test "round trip variable int 2" {


@@ 454,7 447,7 @@ test "round trip variable int 2" {
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encodeVarInt(0x10000, fbs.writer());
    const res = try Decoder.init(std.testing.allocator).decodeVarInt(io.fixedBufferStream(fbs.getWritten()).reader());
    std.testing.expectEqual(res, 0x10000);
    try std.testing.expectEqual(res, 0x10000);
}

test "round trip union with void member" {


@@ 466,5 459,5 @@ test "round trip union with void member" {
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encode(U{ .Hello = 123 }, fbs.writer());
    const res = try Decoder.init(std.testing.allocator).decode(U, io.fixedBufferStream(fbs.getWritten()).reader());
    std.testing.expectEqual(res, U{ .Hello = 123 });
    try std.testing.expectEqual(res, U{ .Hello = 123 });
}

M src/test.zig => src/test.zig +71 -71
@@ 21,16 21,16 @@ test "" {

test "read u8" {
    var r = Decoder.init(testing.allocator);
    expectEqual(try r.decode(u8, io.fixedBufferStream("\x00").reader()), 0);
    expectEqual(try r.decode(u8, io.fixedBufferStream("\xff").reader()), 255);
    expectEqual(try r.decode(u8, io.fixedBufferStream("\x2a").reader()), 42);
    try expectEqual(r.decode(u8, io.fixedBufferStream("\x00").reader()), 0);
    try expectEqual(r.decode(u8, io.fixedBufferStream("\xff").reader()), 255);
    try expectEqual(r.decode(u8, io.fixedBufferStream("\x2a").reader()), 42);
}

test "read bool" {
    var r = Decoder.init(testing.allocator);
    expectEqual(try r.decode(bool, io.fixedBufferStream("\x00").reader()), false);
    expectEqual(try r.decode(bool, io.fixedBufferStream("\xff").reader()), true);
    expectEqual(try r.decode(bool, io.fixedBufferStream("\x01").reader()), true);
    try expectEqual(r.decode(bool, io.fixedBufferStream("\x00").reader()), false);
    try expectEqual(r.decode(bool, io.fixedBufferStream("\xff").reader()), true);
    try expectEqual(r.decode(bool, io.fixedBufferStream("\x01").reader()), true);
}

test "read struct 1" {


@@ 41,9 41,9 @@ test "read struct 1" {
    };

    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x00\x00\x28\x42\x2a\xaa").reader());
    expectEqual(res.a, 42.0);
    expectEqual(res.b, 42);
    expectEqual(res.c, true);
    try expectEqual(res.a, 42.0);
    try expectEqual(res.b, 42);
    try expectEqual(res.c, true);
}

test "read struct 2" {


@@ 54,9 54,9 @@ test "read struct 2" {
    };

    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x00\x00\x28\x42\x2a\x00").reader());
    expectEqual(res.a, 42.0);
    expectEqual(res.b, 42);
    expectEqual(res.c, false);
    try expectEqual(res.a, 42.0);
    try expectEqual(res.b, 42);
    try expectEqual(res.c, false);
}

test "read struct 3" {


@@ 67,9 67,9 @@ test "read struct 3" {
    };

    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x00\x00\x28\x42\x2b\x00").reader());
    expectEqual(res.a, 42.0);
    expectEqual(res.b, 43);
    expectEqual(res.c, false);
    try expectEqual(res.a, 42.0);
    try expectEqual(res.b, 43);
    try expectEqual(res.c, false);
}

test "read struct 4" {


@@ 80,9 80,9 @@ test "read struct 4" {
    };

    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x00\x00\x29\x42\x2b\x00").reader());
    expectEqual(res.a, 42.25);
    expectEqual(res.b, 43);
    expectEqual(res.c, false);
    try expectEqual(res.a, 42.25);
    try expectEqual(res.b, 43);
    try expectEqual(res.c, false);
}

test "read struct 5" {


@@ 93,9 93,9 @@ test "read struct 5" {
    };

    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x00\x00\x00\x00\x00\x00").reader());
    expectEqual(res.a, 0.0);
    expectEqual(res.b, 0);
    expectEqual(res.c, false);
    try expectEqual(res.a, 0.0);
    try expectEqual(res.b, 0);
    try expectEqual(res.c, false);
}

test "read struct 6" {


@@ 106,9 106,9 @@ test "read struct 6" {
    };

    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\xff\xff\xff\xff\xff\xff").reader());
    expect(math.isNan(res.a));
    expectEqual(res.b, 255);
    expectEqual(res.c, true);
    try expect(math.isNan(res.a));
    try expectEqual(res.b, 255);
    try expectEqual(res.c, true);
}

test "read struct with void members" {


@@ 129,13 129,13 @@ test "read enum" {
        c = 0,
    };
    var res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x02").reader());
    expectEqual(res, .a);
    try expectEqual(res, .a);

    res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x01").reader());
    expectEqual(res, .b);
    try expectEqual(res, .b);

    res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x00").reader());
    expectEqual(res, .c);
    try expectEqual(res, .c);
}

test "read invalid enum value" {


@@ 144,7 144,7 @@ test "read invalid enum value" {
        b = 1,
        c = 0,
    };
    expectError(
    try expectError(
        error.InvalidEnumTag,
        Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x03").reader())
    );


@@ 152,47 152,47 @@ test "read invalid enum value" {

test "read optional u8 value" {
    const res = try Decoder.init(testing.allocator).decode(?u8, io.fixedBufferStream("\x01\x2a").reader());
    expectEqual(res, 42);
    try expectEqual(res, 42);
}

test "read optional u8 null" {
    const res = try Decoder.init(testing.allocator).decode(?u8, io.fixedBufferStream("\x00").reader());
    expectEqual(res, null);
    try expectEqual(res, null);
}

test "read u8 array" {
    const res = try Decoder.init(testing.allocator).decode([4]u8, io.fixedBufferStream("\x01\x02\x03\x04").reader());
    const expected = [4]u8{ 1, 2, 3, 4 };
    expectEqual(res, expected);
    try expectEqual(res, expected);
}

test "read u8 slice" {
    const res = try Decoder.init(testing.allocator).decode([]const u8, io.fixedBufferStream("\x04\x01\x02\x03\x04").reader());
    defer testing.allocator.free(res);
    const expected = [_]u8{ 1, 2, 3, 4 };
    expectEqualSlices(u8, expected[0..], res[0..]);
    try expectEqualSlices(u8, expected[0..], res[0..]);
}

test "read 0-length slice" {
    const res = try Decoder.init(testing.allocator).decode([]const u8, io.fixedBufferStream("\x00").reader());
    defer testing.allocator.free(res);
    const expected = [_]u8{};
    expectEqualSlices(u8, expected[0..], res[0..]);
    try expectEqualSlices(u8, expected[0..], res[0..]);
}

test "read map[u8]u8" {
    var r = Decoder.init(testing.allocator);
    defer r.deinit();
    const map = try r.decode(std.AutoHashMap(u8, u8), io.fixedBufferStream("\x02\x01\x02\x03\x04").reader());
    expectEqual(map.get(1) orelse unreachable, 2);
    expectEqual(map.get(3) orelse unreachable, 4);
    try expectEqual(map.get(1) orelse unreachable, 2);
    try expectEqual(map.get(3) orelse unreachable, 4);
}

test "read map[u8]u8 with 0 items" {
    var r = Decoder.init(testing.allocator);
    defer r.deinit();
    const map = try r.decode(std.AutoHashMap(u8, u8), io.fixedBufferStream("\x00").reader());
    expectEqual(map.count(), 0);
    try expectEqual(map.count(), 0);
}

test "read map[string]u8" {


@@ 200,8 200,8 @@ test "read map[string]u8" {
    defer r.deinit();
    const T = std.StringHashMap(u8);
    const map = try r.decode(T, io.fixedBufferStream("\x02\x04zomg\x04\x03lol\x02").reader());
    expectEqual(map.get("lol") orelse unreachable, 2);
    expectEqual(map.get("zomg") orelse unreachable, 4);
    try expectEqual(map.get("lol") orelse unreachable, 2);
    try expectEqual(map.get("zomg") orelse unreachable, 4);
}

test "read map overflow" {


@@ 209,7 209,7 @@ test "read map overflow" {
    defer r.deinit();
    const T = std.StringHashMap(u8);
    const map = r.decode(T, io.fixedBufferStream("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x04zomg\x04\x03lol\x02").reader());
    expectError(error.Overflow, map);
    try expectError(error.Overflow, map);
}

test "read map[u8]void" {


@@ 226,7 226,7 @@ test "read map[u8]void" {
test "read tagged union" {
    const Foo = union(enum) { a: i64, b: bool, c: u8 };
    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream("\x02\x2a").reader());
    expectEqual(Foo{ .c = 42 }, res);
    try expectEqual(Foo{ .c = 42 }, res);
}

test "read untagged union" {


@@ 285,7 285,7 @@ test "write u8" {
    var fbs = io.fixedBufferStream(&buf);
    const x: u8 = 42;
    try Encoder.init().encode(x, fbs.writer());
    expectEqual(fbs.getWritten()[0], x);
    try expectEqual(fbs.getWritten()[0], x);
}

test "write bool" {


@@ 293,8 293,8 @@ test "write bool" {
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encode(false, fbs.writer());
    try Encoder.init().encode(true, fbs.writer());
    expectEqual(fbs.getWritten()[0], 0);
    expectEqual(fbs.getWritten()[1], 1);
    try expectEqual(fbs.getWritten()[0], 0);
    try expectEqual(fbs.getWritten()[1], 1);
}

test "write struct" {


@@ 308,7 308,7 @@ test "write struct" {
    var fbs = io.fixedBufferStream(&buf);
    const expected = "\x00\x00\x28\x42\x2a\x01";
    try Encoder.init().encode(Foo{ .a = 42.0, .b = 42, .c = true }, fbs.writer());
    expect(mem.eql(u8, fbs.getWritten(), expected));
    try expect(mem.eql(u8, fbs.getWritten(), expected));
}

test "write struct with void members" {


@@ 334,7 334,7 @@ test "write enum" {
    var fbs = io.fixedBufferStream(&buf);
    const expected = "\x02";
    try Encoder.init().encode(Foo.c, fbs.writer());
    expect(mem.eql(u8, fbs.getWritten(), expected));
    try expect(mem.eql(u8, fbs.getWritten(), expected));
}

test "write optional u8 value" {


@@ 342,8 342,8 @@ test "write optional u8 value" {
    var fbs = io.fixedBufferStream(&buf);
    var x: ?u8 = 42;
    try Encoder.init().encode(x, fbs.writer());
    expectEqual(fbs.getWritten()[0], @boolToInt(true));
    expectEqual(fbs.getWritten()[1], 42);
    try expectEqual(fbs.getWritten()[0], @boolToInt(true));
    try expectEqual(fbs.getWritten()[1], 42);
}

test "write optional u8 null" {


@@ 351,8 351,8 @@ test "write optional u8 null" {
    var fbs = io.fixedBufferStream(&buf);
    var x: ?u8 = null;
    try Encoder.init().encode(x, fbs.writer());
    expectEqual(fbs.getWritten().len, 1);
    expectEqual(fbs.getWritten()[0], @boolToInt(false));
    try expectEqual(fbs.getWritten().len, 1);
    try expectEqual(fbs.getWritten()[0], @boolToInt(false));
}

test "write optional void" {


@@ 373,8 373,8 @@ test "write u8 array" {
    var x = [4]u8{ 1, 2, 3, 4 };
    try Encoder.init().encode(x, fbs.writer());
    const expected = [_]u8{ 1, 2, 3, 4 };
    expectEqual(fbs.getWritten().len, 4);
    expectEqualSlices(u8, fbs.getWritten()[0..], expected[0..]);
    try expectEqual(fbs.getWritten().len, 4);
    try expectEqualSlices(u8, fbs.getWritten()[0..], expected[0..]);
}

test "write array of void" {


@@ 397,9 397,9 @@ test "write slice" {
    const slajs: []const u8 = expected[0..];

    try Encoder.init().encode(slajs, fbs.writer());
    expectEqual(fbs.getWritten().len, 5);
    expectEqual(fbs.getWritten()[0], 4);
    expectEqualSlices(u8, fbs.getWritten()[1..], expected[0..]);
    try expectEqual(fbs.getWritten().len, 5);
    try expectEqual(fbs.getWritten()[0], 4);
    try expectEqualSlices(u8, fbs.getWritten()[1..], expected[0..]);
}

test "write slice of void" {


@@ 426,8 426,8 @@ test "write map[u8]u8" {

    try Encoder.init().encode(map, fbs.writer());
    const expected = "\x02\x01\x02\x03\x04";
    expectEqual(fbs.getWritten().len, 5);
    expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
    try expectEqual(fbs.getWritten().len, 5);
    try expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
}

test "write map[string]u8" {


@@ 443,8 443,8 @@ test "write map[string]u8" {
    try Encoder.init().encode(map, fbs.writer());
    // 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..]);
    try expectEqual(fbs.getWritten().len, 12);
    try expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
}

test "write map[u8]void" {


@@ 468,8 468,8 @@ test "write tagged union" {

    try Encoder.init().encode(foo, fbs.writer());
    const expected = "\x00\x2a\x00\x00\x00\x00\x00\x00\x00";
    expectEqual(fbs.getWritten().len, 1 + @sizeOf(i64));
    expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
    try expectEqual(fbs.getWritten().len, 1 + @sizeOf(i64));
    try expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
}

test "write tagged union with void member active" {


@@ 481,7 481,7 @@ test "write tagged union with void member active" {

    try Encoder.init().encode(foo, fbs.writer());
    const expected = "\x01";
    expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
    try expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
}

test "write untagged union" {


@@ 549,7 549,7 @@ test "round trip u8" {
    const x: u8 = 42;
    try Encoder.init().encode(x, fbs.writer());
    const res = try Decoder.init(testing.allocator).decode(u8, io.fixedBufferStream(fbs.getWritten()).reader());
    expectEqual(res, x);
    try expectEqual(res, x);
}

test "round trip struct" {


@@ 564,7 564,7 @@ test "round trip struct" {
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encode(foo, fbs.writer());
    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream(fbs.getWritten()).reader());
    expectEqual(res, foo);
    try expectEqual(res, foo);
}

test "round trip enum" {


@@ 578,7 578,7 @@ test "round trip enum" {
    var fbs = io.fixedBufferStream(&buf);
    try Encoder.init().encode(Foo.c, fbs.writer());
    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream(fbs.getWritten()).reader());
    expectEqual(res, Foo.c);
    try expectEqual(res, Foo.c);
}

test "round trip i64 array" {


@@ 587,7 587,7 @@ test "round trip i64 array" {
    const arr = [_]i64{ -1, 1, math.maxInt(i64), math.minInt(i64) };
    try Encoder.init().encode(arr, fbs.writer());
    const res = try Decoder.init(testing.allocator).decode(@TypeOf(arr), io.fixedBufferStream(fbs.getWritten()).reader());
    expectEqualSlices(i64, arr[0..], res[0..]);
    try expectEqualSlices(i64, arr[0..], res[0..]);
}

test "round trip i32 slice" {


@@ 599,7 599,7 @@ test "round trip i32 slice" {
    try Encoder.init().encode(slajs, fbs.writer());
    const res = try Decoder.init(testing.allocator).decode([]const i32, io.fixedBufferStream(fbs.getWritten()).reader());
    defer testing.allocator.free(res);
    expectEqualSlices(i32, slajs, res[0..]);
    try expectEqualSlices(i32, slajs, res[0..]);
}

test "round trip tagged union" {


@@ 615,7 615,7 @@ test "round trip tagged union" {
    try Encoder.init().encode(foo, fbs.writer());
    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream(fbs.getWritten()).reader());

    expectEqual(foo, res);
    try expectEqual(foo, res);
}

test "round trip tagged union with void member active" {


@@ 631,7 631,7 @@ test "round trip tagged union with void member active" {
    try Encoder.init().encode(foo, fbs.writer());
    const res = try Decoder.init(testing.allocator).decode(Foo, io.fixedBufferStream(fbs.getWritten()).reader());

    expectEqual(foo, res);
    try expectEqual(foo, res);
}

test "bare.Decoder frees its memory" {


@@ 765,18 765,18 @@ fn testCompileError(comptime code: []const u8, comptime expected_error: []const 
    const term = try proc.wait();
    defer testing.allocator.free(stderr);
    try checkErrorMessage(stderr, expected_error);
    expectEqual(term.Exited, 1);
    try expectEqual(term.Exited, 1);
}

fn checkErrorMessage(haystack: []const u8, message: []const u8) !void {
    const errorstart = "error: ";
    const idx = mem.indexOf(u8, haystack, errorstart) orelse {
        expect(false);
        try expect(false);
        return;
    };
    const errorslice = haystack[idx + errorstart.len..];
    const starts_with = mem.startsWith(u8, errorslice, message);
    if (!starts_with)
        warn("\nunexpected output: {s}; expected: {s}", .{ errorslice, message });
    expect(starts_with);
    try expect(starts_with);
}