~alva/zig-bare

1d3dc73ec285ede39926d58ebbeaafa823833f0d — owl 6 months ago b7a656d
fix `var`nings
4 files changed, 60 insertions(+), 123 deletions(-)

M README.md
M build.zig.zon
M src/bare.zig
M src/test.zig
M README.md => README.md +2 -2
@@ 47,8 47,8 @@ test "example" {
    var buf: [12]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);

    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();

    var e = encoder(writer);
    try e.encode(x);

M build.zig.zon => build.zig.zon +2 -2
@@ 4,8 4,8 @@
    .paths = .{""},
    .dependencies = .{
        .varint = .{
            .url = "https://git.sr.ht/~alva/zig-varint/archive/f72dd5fd.tar.gz",
            .hash = "1220cd18cd3e4f76e09e11e20461ad80dfe6e211996dc4a0afa41b1d9b270732b0d0",
            .url = "https://git.sr.ht/~alva/zig-varint/archive/f9e5b7f.tar.gz",
            .hash = "1220136609b836df8ff707062b100395f49d6c87187c3e74312c223618ad69e9a9ab",
        },
    },
}

M src/bare.zig => src/bare.zig +5 -4
@@ 1,5 1,5 @@
const std = @import("std");
const varint = @import("varint.zig");
const varint = @import("varint");
const io = std.io;
const math = std.math;
const mem = std.mem;


@@ 163,7 163,8 @@ pub fn Decoder(comptime ReaderType: type) type {
            if (comptime !isValidHashMapKeyType(K))
                @compileError("unsupported hashmap key type " ++ @typeName(K));

            var i = try self.decodeVarUint();
            const i = try self.decodeVarUint();

            if (std.math.maxInt(T.Size) < i)
                return DecodeError.Overflow;



@@ 382,8 383,8 @@ test "round trip union with void member" {
    };
    var buf: [4]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(U{ .Hello = 123 });
    var d = decoder(std.testing.allocator, reader);

M src/test.zig => src/test.zig +51 -115
@@ 26,7 26,7 @@ test "read u8" {

    inline for (cases) |pair| {
        var fbs = io.fixedBufferStream(pair[0]);
        var reader = fbs.reader();
        const reader = fbs.reader();
        var d = decoder(testing.allocator, reader);
        defer d.deinit();
        try expectEqual(@as(u8, pair[1]), try d.decode(u8));


@@ 35,7 35,7 @@ test "read u8" {

test "read bool" {
    var fbs = io.fixedBufferStream("\x00");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    try expectEqual(d.decode(bool), false);


@@ 53,7 53,7 @@ test "read struct 1" {
    };

    var fbs = io.fixedBufferStream("\x00\x00\x28\x42\x2a\xaa");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Foo);


@@ 70,7 70,7 @@ test "read struct 2" {
    };

    var fbs = io.fixedBufferStream("\x00\x00\x28\x42\x2a\x00");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Foo);


@@ 87,7 87,7 @@ test "read struct 3" {
    };

    var fbs = io.fixedBufferStream("\x00\x00\x28\x42\x2b\x00");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Foo);


@@ 104,7 104,7 @@ test "read struct 4" {
    };

    var fbs = io.fixedBufferStream("\x00\x00\x29\x42\x2b\x00");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Foo);


@@ 121,7 121,7 @@ test "read struct 5" {
    };

    var fbs = io.fixedBufferStream("\x00\x00\x00\x00\x00\x00");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Foo);


@@ 138,7 138,7 @@ test "read struct 6" {
    };

    var fbs = io.fixedBufferStream("\xff\xff\xff\xff\xff\xff");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Foo);


@@ 167,7 167,7 @@ test "read enum" {
        c = 0,
    };
    var fbs = io.fixedBufferStream("\x02");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    var res = try d.decode(Foo);


@@ 189,14 189,14 @@ test "read invalid enum value" {
        c = 0,
    };
    var fbs = io.fixedBufferStream("\x03");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    try expectError(error.InvalidEnumTag, d.decode(Foo));
}

test "read optional u8 value" {
    var fbs = io.fixedBufferStream("\x01\x2a");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(?u8);


@@ 205,7 205,7 @@ test "read optional u8 value" {

test "read optional u8 null" {
    var fbs = io.fixedBufferStream("\x00");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(?u8);


@@ 214,7 214,7 @@ test "read optional u8 null" {

test "read u8 array" {
    var fbs = io.fixedBufferStream("\x01\x02\x03\x04");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode([4]u8);


@@ 224,7 224,7 @@ test "read u8 array" {

test "read u8 slice" {
    var fbs = io.fixedBufferStream("\x04\x01\x02\x03\x04");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode([]const u8);


@@ 234,7 234,7 @@ test "read u8 slice" {

test "read 0-length slice" {
    var fbs = io.fixedBufferStream("\x00");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode([]const u8);


@@ 245,7 245,7 @@ test "read 0-length slice" {

test "read map[u8]u8" {
    var fbs = io.fixedBufferStream("\x02\x01\x02\x03\x04");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const map = try d.decode(std.AutoHashMap(u8, u8));


@@ 255,7 255,7 @@ test "read map[u8]u8" {

test "read map[u8]u8 with 0 items" {
    var fbs = io.fixedBufferStream("\x00");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const map = try d.decode(std.AutoHashMap(u8, u8));


@@ 264,7 264,7 @@ test "read map[u8]u8 with 0 items" {

test "read map[string]u8" {
    var fbs = io.fixedBufferStream("\x02\x04zomg\x04\x03lol\x02");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const T = std.StringHashMap(u8);


@@ 275,7 275,7 @@ test "read map[string]u8" {

test "read map overflow" {
    var fbs = io.fixedBufferStream("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x04zomg\x04\x03lol\x02");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const T = std.StringHashMap(u8);


@@ 299,7 299,7 @@ test "read map[u8]void" {
test "read tagged union" {
    const Foo = union(enum) { a: i64, b: bool, c: u8 };
    var fbs = io.fixedBufferStream("\x02\x2a");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Foo);


@@ 370,7 370,7 @@ test "read unsupported pointer type" {
test "write u8" {
    var buf: [1]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var writer = fbs.writer();
    const writer = fbs.writer();
    const x: u8 = 42;
    var e = encoder(writer);
    try e.encode(x);


@@ 380,7 380,7 @@ test "write u8" {
test "write bool" {
    var buf: [2]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var writer = fbs.writer();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(false);
    try e.encode(true);


@@ 398,7 398,7 @@ test "write struct" {
    var buf: [@sizeOf(Foo)]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    const expected = "\x00\x00\x28\x42\x2a\x01";
    var writer = fbs.writer();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(Foo{ .a = 42.0, .b = 42, .c = true });
    try expect(mem.eql(u8, fbs.getWritten(), expected));


@@ 427,7 427,7 @@ test "write enum" {
    var buf: [@sizeOf(Foo)]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    const expected = "\x02";
    var writer = fbs.writer();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(Foo.c);
    try expect(mem.eql(u8, fbs.getWritten(), expected));


@@ 436,8 436,8 @@ test "write enum" {
test "write optional u8 value" {
    var buf: [2]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var writer = fbs.writer();
    var x: ?u8 = 42;
    const writer = fbs.writer();
    const x: ?u8 = 42;
    var e = encoder(writer);
    try e.encode(x);
    try expectEqual(fbs.getWritten()[0], @intFromBool(true));


@@ 447,8 447,8 @@ test "write optional u8 value" {
test "write optional u8 null" {
    var buf: [2]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var x: ?u8 = null;
    var writer = fbs.writer();
    const x: ?u8 = null;
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(x);
    try expectEqual(fbs.getWritten().len, 1);


@@ 471,8 471,8 @@ test "write optional void" {
test "write u8 array" {
    var buf: [4]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var x = [4]u8{ 1, 2, 3, 4 };
    var writer = fbs.writer();
    const x = [4]u8{ 1, 2, 3, 4 };
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(x);
    const expected = [_]u8{ 1, 2, 3, 4 };


@@ 500,7 500,7 @@ test "write slice" {
    const expected = [_]u8{ 1, 2, 3, 4 };
    const slajs: []const u8 = &expected;

    var writer = fbs.writer();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(slajs);
    try expectEqual(fbs.getWritten().len, 5);


@@ 531,7 531,7 @@ test "write map[u8]u8" {
    _ = try map.put(1, 2);
    _ = try map.put(3, 4);

    var writer = fbs.writer();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(map);
    const expected = "\x02\x01\x02\x03\x04";


@@ 549,7 549,7 @@ test "write map[string]u8" {
    _ = try map.put("lol", 2);
    _ = try map.put("zomg", 4);

    var writer = fbs.writer();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(map);
    const expected = "\x02\x03lol\x02\x04zomg\x04";


@@ 577,7 577,7 @@ test "write tagged union" {
    const Foo = union(enum) { a: i64, b: bool, c: u8 };
    const foo = Foo{ .a = 42 };

    var writer = fbs.writer();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(foo);
    const expected = "\x00\x2a\x00\x00\x00\x00\x00\x00\x00";


@@ 592,82 592,18 @@ test "write tagged union with void member active" {
    const Foo = union(enum) { a: i64, b: void, c: u8 };
    const foo = Foo{ .b = {} };

    var writer = fbs.writer();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(foo);
    const expected = "\x01";
    try expectEqualSlices(u8, fbs.getWritten(), expected);
}

test "write untagged union" {
    try testCompileError(
        \\pub fn main() !void {
        \\  const Foo = union { a: u8, b: void };
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  _ = try e.encode(Foo{ .a = 2 });
        \\}
    , "only tagged unions are supported");
}

test "write void" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode({});
        \\}
    , "unsupported type void");
}

test "write unsupported integer type" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  const foo: u7 = 7;
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode(foo);
        \\}
    , "unsupported integer type u7");
}

test "write unsupported float type" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var writer = fbs.writer();
        \\  const foo: f16 = 4.2;
        \\  var e = encoder(writer);
        \\  try e.encode(foo);
        \\}
    , "unsupported float type f16");
}

test "write unsupported pointer type" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var writer = fbs.writer();
        \\  const x: u8 = 2;
        \\  const foo: *const u8 = &x;
        \\  var e = encoder(writer);
        \\  try e.encode(foo);
        \\}
    , "slices are the only supported pointer type");
}

test "round trip u8" {
    var buf: [0x100]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();
    const x: u8 = 42;
    var e = encoder(writer);
    try e.encode(x);


@@ 688,8 624,8 @@ test "round trip struct" {
    const foo = Foo{ .a = 42.0, .b = 3, .c = true };
    var buf: [@sizeOf(Foo)]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(foo);
    var d = decoder(testing.allocator, reader);


@@ 708,8 644,8 @@ test "round trip enum" {

    var buf: [@sizeOf(Foo)]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();
    var e = encoder(writer);
    try e.encode(Foo.c);
    var d = decoder(testing.allocator, reader);


@@ 722,8 658,8 @@ test "round trip enum" {
test "round trip i64 array" {
    var buf: [0x100]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();
    const arr = [_]i64{ -1, 1, math.maxInt(i64), math.minInt(i64) };
    var e = encoder(writer);
    try e.encode(arr);


@@ 737,8 673,8 @@ test "round trip i64 array" {
test "round trip i32 slice" {
    var buf: [0x100]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();

    var e = encoder(writer);



@@ 762,8 698,8 @@ test "round trip tagged union" {
    const foo = Foo{ .b = 42 };
    var buf: [0x100]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();

    var e = encoder(writer);
    try e.encode(foo);


@@ 784,8 720,8 @@ test "round trip tagged union with void member active" {
    const foo = Foo{ .b = {} };
    var buf: [0x100]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    var reader = fbs.reader();
    var writer = fbs.writer();
    const reader = fbs.reader();
    const writer = fbs.writer();

    var e = encoder(writer);
    try e.encode(foo);


@@ 800,7 736,7 @@ test "round trip tagged union with void member active" {
test "invalid union returns error" {
    const Foo = union(enum) { a: i64, b: bool, c: u8 };
    var fbs = io.fixedBufferStream("\x09\x2a");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    const res = d.decode(Foo);
    try expectError(error.InvalidUnion, res);


@@ 808,7 744,7 @@ test "invalid union returns error" {

test "bare.Decoder frees its memory" {
    var fbs = io.fixedBufferStream("\x02\x01\x02\x03\x04");
    var reader = fbs.reader();
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    _ = try d.decode(std.AutoHashMap(u8, u8));