@@ 7,6 7,8 @@ const testing = std.testing;
const bare = @import("bare.zig");
const Decoder = bare.Decoder;
const Encoder = bare.Encoder;
+const decoder = bare.decoder;
+const encoder = bare.encoder;
const DecodeError = bare.DecodeError;
const expect = testing.expect;
@@ 28,7 30,7 @@ test "read u8" {
inline for (cases) |pair| {
var fbs = io.fixedBufferStream(pair[0]);
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
try expectEqual(@as(u8, pair[1]), try d.decode(u8));
}
@@ 37,7 39,7 @@ test "read u8" {
test "read bool" {
var fbs = io.fixedBufferStream("\x00");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
try expectEqual(d.decode(bool), false);
fbs = io.fixedBufferStream("\xff");
@@ 55,7 57,7 @@ test "read struct 1" {
var fbs = io.fixedBufferStream("\x00\x00\x28\x42\x2a\xaa");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(Foo);
try expectEqual(res.a, 42.0);
@@ 72,7 74,7 @@ test "read struct 2" {
var fbs = io.fixedBufferStream("\x00\x00\x28\x42\x2a\x00");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(Foo);
try expectEqual(res.a, 42.0);
@@ 89,7 91,7 @@ test "read struct 3" {
var fbs = io.fixedBufferStream("\x00\x00\x28\x42\x2b\x00");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(Foo);
try expectEqual(res.a, 42.0);
@@ 106,7 108,7 @@ test "read struct 4" {
var fbs = io.fixedBufferStream("\x00\x00\x29\x42\x2b\x00");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(Foo);
try expectEqual(res.a, 42.25);
@@ 123,7 125,7 @@ test "read struct 5" {
var fbs = io.fixedBufferStream("\x00\x00\x00\x00\x00\x00");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(Foo);
try expectEqual(res.a, 0.0);
@@ 140,7 142,7 @@ test "read struct 6" {
var fbs = io.fixedBufferStream("\xff\xff\xff\xff\xff\xff");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(Foo);
try expect(math.isNan(res.a));
@@ 169,7 171,7 @@ test "read enum" {
};
var fbs = io.fixedBufferStream("\x02");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
var res = try d.decode(Foo);
try expectEqual(res, .a);
@@ 191,14 193,14 @@ test "read invalid enum value" {
};
var fbs = io.fixedBufferStream("\x03");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
try expectError(error.InvalidEnumTag, d.decode(Foo));
}
test "read optional u8 value" {
var fbs = io.fixedBufferStream("\x01\x2a");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(?u8);
try expectEqual(res, 42);
@@ 207,7 209,7 @@ test "read optional u8 value" {
test "read optional u8 null" {
var fbs = io.fixedBufferStream("\x00");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(?u8);
try expectEqual(res, null);
@@ 216,7 218,7 @@ test "read optional u8 null" {
test "read u8 array" {
var fbs = io.fixedBufferStream("\x01\x02\x03\x04");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode([4]u8);
const expected = [4]u8{ 1, 2, 3, 4 };
@@ 226,7 228,7 @@ test "read u8 array" {
test "read u8 slice" {
var fbs = io.fixedBufferStream("\x04\x01\x02\x03\x04");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode([]const u8);
const expected = [_]u8{ 1, 2, 3, 4 };
@@ 236,7 238,7 @@ test "read u8 slice" {
test "read 0-length slice" {
var fbs = io.fixedBufferStream("\x00");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode([]const u8);
defer testing.allocator.free(res);
@@ 247,7 249,7 @@ test "read 0-length slice" {
test "read map[u8]u8" {
var fbs = io.fixedBufferStream("\x02\x01\x02\x03\x04");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const map = try d.decode(std.AutoHashMap(u8, u8));
try expectEqual(map.get(1) orelse unreachable, 2);
@@ 257,7 259,7 @@ test "read map[u8]u8" {
test "read map[u8]u8 with 0 items" {
var fbs = io.fixedBufferStream("\x00");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const map = try d.decode(std.AutoHashMap(u8, u8));
try expectEqual(map.count(), 0);
@@ 266,7 268,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();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const T = std.StringHashMap(u8);
const map = try d.decode(T);
@@ 277,7 279,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();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const T = std.StringHashMap(u8);
const map = d.decode(T);
@@ 301,7 303,7 @@ test "read tagged union" {
const Foo = union(enum) { a: i64, b: bool, c: u8 };
var fbs = io.fixedBufferStream("\x02\x2a");
var reader = fbs.reader();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
const res = try d.decode(Foo);
try expectEqual(Foo{ .c = 42 }, res);
@@ 373,7 375,7 @@ test "write u8" {
var fbs = io.fixedBufferStream(&buf);
var writer = fbs.writer();
const x: u8 = 42;
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(x);
try expectEqual(fbs.getWritten()[0], x);
}
@@ 382,7 384,7 @@ test "write bool" {
var buf: [2]u8 = undefined;
var fbs = io.fixedBufferStream(&buf);
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(false);
try e.encode(true);
try expectEqual(fbs.getWritten()[0], 0);
@@ 400,7 402,7 @@ test "write struct" {
var fbs = io.fixedBufferStream(&buf);
const expected = "\x00\x00\x28\x42\x2a\x01";
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(Foo{ .a = 42.0, .b = 42, .c = true });
try expect(mem.eql(u8, fbs.getWritten(), expected));
}
@@ 412,7 414,7 @@ test "write struct with void members" {
\\ var buf: [0x100]u8 = undefined;
\\ var fbs = io.fixedBufferStream(&buf);
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(Foo{ .x = 1, .y = {} });
\\}
, "unsupported type");
@@ 429,7 431,7 @@ test "write enum" {
var fbs = io.fixedBufferStream(&buf);
const expected = "\x02";
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(Foo.c);
try expect(mem.eql(u8, fbs.getWritten(), expected));
}
@@ 439,7 441,7 @@ test "write optional u8 value" {
var fbs = io.fixedBufferStream(&buf);
var writer = fbs.writer();
var x: ?u8 = 42;
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(x);
try expectEqual(fbs.getWritten()[0], @boolToInt(true));
try expectEqual(fbs.getWritten()[1], 42);
@@ 450,7 452,7 @@ test "write optional u8 null" {
var fbs = io.fixedBufferStream(&buf);
var x: ?u8 = null;
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(x);
try expectEqual(fbs.getWritten().len, 1);
try expectEqual(fbs.getWritten()[0], @boolToInt(false));
@@ 463,7 465,7 @@ test "write optional void" {
\\ var fbs = io.fixedBufferStream(&buf);
\\ const foo: ?void = null;
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(foo);
\\}
, "unsupported type");
@@ 474,7 476,7 @@ test "write u8 array" {
var fbs = io.fixedBufferStream(&buf);
var x = [4]u8{ 1, 2, 3, 4 };
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(x);
const expected = [_]u8{ 1, 2, 3, 4 };
try expectEqual(fbs.getWritten().len, 4);
@@ 488,7 490,7 @@ test "write array of void" {
\\ var fbs = io.fixedBufferStream(&buf);
\\ const foo: [4]void = .{ {}, {}, {}, {} };
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(foo);
\\}
, "unsupported type");
@@ 502,7 504,7 @@ test "write slice" {
const slajs: []const u8 = &expected;
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(slajs);
try expectEqual(fbs.getWritten().len, 5);
try expectEqual(fbs.getWritten()[0], 4);
@@ 516,7 518,7 @@ test "write slice of void" {
\\ var fbs = io.fixedBufferStream(&buf);
\\ const foo: []const void = &[_]void{ {}, {} };
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(foo);
\\}
, "unsupported type void");
@@ 533,7 535,7 @@ test "write map[u8]u8" {
_ = try map.put(3, 4);
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(map);
const expected = "\x02\x01\x02\x03\x04";
try expectEqual(fbs.getWritten().len, 5);
@@ 551,7 553,7 @@ test "write map[string]u8" {
_ = try map.put("zomg", 4);
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(map);
// TODO: Fix this; later versions of std.HashMap preserve insertion order.
const expected = "\x02\x04zomg\x04\x03lol\x02";
@@ 566,7 568,7 @@ test "write map[u8]void" {
\\ var fbs = io.fixedBufferStream(&buf);
\\ const foo = std.AutoHashMap(u8, void);
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(foo);
\\}
, "unsupported type");
@@ 580,7 582,7 @@ test "write tagged union" {
const foo = Foo{ .a = 42 };
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(foo);
const expected = "\x00\x2a\x00\x00\x00\x00\x00\x00\x00";
try expectEqual(fbs.getWritten().len, 1 + @sizeOf(i64));
@@ 595,7 597,7 @@ test "write tagged union with void member active" {
const foo = Foo{ .b = {} };
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(foo);
const expected = "\x01";
try expectEqualSlices(u8, fbs.getWritten(), expected);
@@ 607,7 609,7 @@ test "write untagged union" {
\\ const Foo = union { a: u8, b: void };
\\ var fbs = io.fixedBufferStream("lol");
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ _ = try e.encode(Foo{ .a = 2 });
\\}
, "only tagged unions are supported");
@@ 619,7 621,7 @@ test "write void" {
\\ var buf: [0x100]u8 = undefined;
\\ var fbs = io.fixedBufferStream(&buf);
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode({});
\\}
, "unsupported type void");
@@ 632,7 634,7 @@ test "write unsupported integer type" {
\\ var fbs = io.fixedBufferStream(&buf);
\\ const foo: u7 = 7;
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(foo);
\\}
, "unsupported integer type u7");
@@ 645,7 647,7 @@ test "write unsupported float type" {
\\ var fbs = io.fixedBufferStream(&buf);
\\ var writer = fbs.writer();
\\ const foo: f16 = 4.2;
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(foo);
\\}
, "unsupported float type f16");
@@ 659,7 661,7 @@ test "write unsupported pointer type" {
\\ var writer = fbs.writer();
\\ const x: u8 = 2;
\\ const foo: *const u8 = &x;
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(foo);
\\}
, "slices are the only supported pointer type");
@@ 671,9 673,9 @@ test "round trip u8" {
var reader = fbs.reader();
var writer = fbs.writer();
const x: u8 = 42;
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(x);
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
fbs = io.fixedBufferStream(fbs.getWritten());
const res = try d.decode(u8);
@@ 692,9 694,9 @@ test "round trip struct" {
var fbs = io.fixedBufferStream(&buf);
var reader = fbs.reader();
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(foo);
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
fbs = io.fixedBufferStream(fbs.getWritten());
const res = try d.decode(Foo);
@@ 712,9 714,9 @@ test "round trip enum" {
var fbs = io.fixedBufferStream(&buf);
var reader = fbs.reader();
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(Foo.c);
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
fbs = io.fixedBufferStream(fbs.getWritten());
const res = try d.decode(Foo);
@@ 727,9 729,9 @@ test "round trip i64 array" {
var reader = fbs.reader();
var writer = fbs.writer();
const arr = [_]i64{ -1, 1, math.maxInt(i64), math.minInt(i64) };
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(arr);
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
fbs = io.fixedBufferStream(fbs.getWritten());
const res = try d.decode(@TypeOf(arr));
@@ 742,13 744,13 @@ test "round trip i32 slice" {
var reader = fbs.reader();
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
const expected = [4]i32{ -1, 2, math.maxInt(i32), math.minInt(i32) };
const slajs: []const i32 = &expected;
try e.encode(slajs);
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
fbs = io.fixedBufferStream(fbs.getWritten());
const res = try d.decode([]const i32);
@@ 767,9 769,9 @@ test "round trip tagged union" {
var reader = fbs.reader();
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(foo);
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
fbs = io.fixedBufferStream(fbs.getWritten());
const res = try d.decode(Foo);
@@ 789,9 791,9 @@ test "round trip tagged union with void member active" {
var reader = fbs.reader();
var writer = fbs.writer();
- var e = Encoder(@TypeOf(writer)).init(writer);
+ var e = encoder(writer);
try e.encode(foo);
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
fbs = io.fixedBufferStream(fbs.getWritten());
const res = try d.decode(Foo);
@@ 803,7 805,7 @@ 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();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
const res = d.decode(Foo);
try expectError(error.InvalidUnion, res);
}
@@ 811,7 813,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();
- var d = Decoder(@TypeOf(reader)).init(reader, testing.allocator);
+ var d = decoder(reader, testing.allocator);
defer d.deinit();
_ = try d.decode(std.AutoHashMap(u8, u8));
fbs = io.fixedBufferStream("\x04\x01\x02\x03\x04");
@@ 838,7 840,7 @@ test "invariant: fixed-length array must have length > 0 (write)" {
\\ var fbs = io.fixedBufferStream(&buf);
\\ var foo = [0]u8{};
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(foo);
\\}
, "array length must be at least 1");
@@ 864,7 866,7 @@ test "invariant: structs must have at least 1 field (write)" {
\\ var buf: [0x100]u8 = undefined;
\\ var fbs = io.fixedBufferStream(&buf);
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ try e.encode(Foo{});
\\}
, "structs must have 1 or more fields");
@@ 890,7 892,7 @@ test "invariant: unions must have at least 1 field (write)" {
\\ var buf: [0x100]u8 = undefined;
\\ var fbs = io.fixedBufferStream(&buf);
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ const foo = Foo{};
\\ _ = try e.encode(foo);
\\}
@@ 905,7 907,7 @@ test "invariant: hashmap keys must be of primitive type" {
\\ var buf: [0x100]u8 = undefined;
\\ var fbs = io.fixedBufferStream(&buf);
\\ var writer = fbs.writer();
- \\ var e = Encoder(@TypeOf(writer)).init(writer);
+ \\ var e = encoder(writer);
\\ _ = try e.encode(hm);
\\}
, "unsupported hashmap key type [64]u8");
@@ 940,6 942,8 @@ const boilerplate =
\\const bare = @import("zig-bare.zig");
\\const Decoder = bare.Decoder;
\\const Encoder = bare.Encoder;
+ \\const decoder = bare.decoder;
+ \\const encoder = bare.encoder;
;
var test_file_seq: usize = 1;