authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-20 11:23:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-20 11:23:12-07:00
loge4abdf5a133ac4822644da1dabd5437ac751d78d
treec7b4a8eab3878c89ae6561999839a949aaaf1857
parentf657767b600064c520fac0e72da6016508a8b601

std.Io.Reader: fix takeStruct/peekStruct packed

closes #24516

1 files changed, 23 insertions(+), 2 deletions(-)

lib/std/Io/Reader.zig+23-2
...@@ -1148,7 +1148,7 @@ pub inline fn takeStruct(r: *Reader, comptime T: type, endian: std.builtin.Endia...@@ -1148,7 +1148,7 @@ pub inline fn takeStruct(r: *Reader, comptime T: type, endian: std.builtin.Endia
1148 return res;1148 return res;
1149 },1149 },
1150 .@"packed" => {1150 .@"packed" => {
1151 return takeInt(r, info.backing_integer.?, endian);1151 return @bitCast(try takeInt(r, info.backing_integer.?, endian));
1152 },1152 },
1153 },1153 },
1154 else => @compileError("not a struct"),1154 else => @compileError("not a struct"),
...@@ -1173,7 +1173,7 @@ pub inline fn peekStruct(r: *Reader, comptime T: type, endian: std.builtin.Endia...@@ -1173,7 +1173,7 @@ pub inline fn peekStruct(r: *Reader, comptime T: type, endian: std.builtin.Endia
1173 return res;1173 return res;
1174 },1174 },
1175 .@"packed" => {1175 .@"packed" => {
1176 return peekInt(r, info.backing_integer.?, endian);1176 return @bitCast(try peekInt(r, info.backing_integer.?, endian));
1177 },1177 },
1178 },1178 },
1179 else => @compileError("not a struct"),1179 else => @compileError("not a struct"),
...@@ -1724,6 +1724,27 @@ test "takeDelimiterInclusive when it rebases" {...@@ -1724,6 +1724,27 @@ test "takeDelimiterInclusive when it rebases" {
1724 }1724 }
1725}1725}
17261726
1727test "takeStruct and peekStruct packed" {
1728 var r: Reader = .fixed(&.{ 0b11110000, 0b00110011 });
1729 const S = packed struct(u16) { a: u2, b: u6, c: u7, d: u1 };
1730
1731 try testing.expectEqual(@as(S, .{
1732 .a = 0b11,
1733 .b = 0b001100,
1734 .c = 0b1110000,
1735 .d = 0b1,
1736 }), try r.peekStruct(S, .big));
1737
1738 try testing.expectEqual(@as(S, .{
1739 .a = 0b11,
1740 .b = 0b001100,
1741 .c = 0b1110000,
1742 .d = 0b1,
1743 }), try r.takeStruct(S, .big));
1744
1745 try testing.expectError(error.EndOfStream, r.takeStruct(S, .little));
1746}
1747
1727/// Provides a `Reader` implementation by passing data from an underlying1748/// Provides a `Reader` implementation by passing data from an underlying
1728/// reader through `Hasher.update`.1749/// reader through `Hasher.update`.
1729///1750///