Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/msgpack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub const Unpacker = struct {
return unpackStruct(self.reader, self.allocator, T);
}

pub fn readUnion(self: Unpacker, comptime T: type) !?T {
pub fn readUnion(self: Unpacker, comptime T: type) !T {
return unpackUnion(self.reader, self.allocator, T);
}

Expand Down Expand Up @@ -487,3 +487,25 @@ test "unpacker readBinary reads bin8" {
defer std.testing.allocator.free(value);
try std.testing.expectEqualSlices(u8, "abc", value);
}

test "unpacker readUnion" {
// `refAllDecls` does not instantiate generic functions, so a signature
// mismatch here stays invisible until something actually calls it.
const Value = union(enum) { a: u8, b: u16 };

const packed_union_a = [_]u8{ 0x81, 0xa1, 'a', 0x01 };
var reader = std.Io.Reader.fixed(&packed_union_a);
try std.testing.expectEqual(
Value{ .a = 1 },
try unpacker(&reader, std.testing.allocator).readUnion(Value),
);

// An optional union is expressed by asking for `?Value`, which
// `unpackUnion` already handles.
const packed_null_union = [_]u8{0xc0};
var null_reader = std.Io.Reader.fixed(&packed_null_union);
try std.testing.expectEqual(
@as(?Value, null),
try unpacker(&null_reader, std.testing.allocator).readUnion(?Value),
);
}
Loading