Support packed structs in std.io.Reader and std.io.Writer
intended to close #12960
successor to https://github.com/ziglang/zig/pull/21562
Summary
This PR adds support for packed structs in std.io.Reader and std.io.Writer.
Some rationale:
- I chose to not read/write padding bytes of packed structs because I think that most users are using packed structs to avoid padding, and thus the padding due to alignment would be annoying.
- I chose to restrict packed struct parameters to having a
@bitSizeOfthat is a multiple of eight because the user is using the reader / writer on a byte stream, and should be expected to add padding fields to the packed struct if they wish to have padding to align to bytes. - For endianness conversion, I chose to use
std.mem.reverseinstead of byteswap on the backing integer because I think it declares my intent more precisely: to reverse the bytes that underly the packed struct. It declares explicitly that I am relying on the big endian representation being the bytes reversal of the little endian representation. Note that this implementation may need to be changed if packed struct can later contain arrays. https://github.com/ziglang/zig/issues/12547
Questions
~1. Should I be using @compileError as I am in the switches? Or perhaps unreachable?~
Test Plan The unit tests added with this PR should pass.
Feedback welcome, as always!
There seems to be precedent for using @compileError like I am. For example:
https://github.com/ziglang/zig/blob/d23db9427b82d7dc798442accf34729c16e92f2d/lib/std/bit_set.zig#L322
@rohlem @rootbeer I'm not sure whether I should be clinking "resolve conversation". I would generally expect the conversation creator to click resolve when they feel it is resolved? Might be worth a mention in the contributing guidelines.
@rohlem @rootbeer I'm not sure whether I should be clinking "resolve conversation". I would generally expect the conversation creator to click resolve when they feel it is resolved? Might be worth a mention in the contributing guidelines.
Yeah, I'd resolve them if I could... According to GitHub docs only PR authors or folks with write access to the repo can resolve a conversation. So in this specific case, all my comments are addressed. Sorry for the extra hassle.
given that packed struct equality operator just got merged https://github.com/ziglang/zig/pull/21679
I will convert my tests to use testing.expectEqual instead of testing.expectEqualDeep, either as soon as I can finish building from source or get my hands on today's nightly.
There seems to be precedent for using
@compileErrorlike I am. For example:https://github.com/ziglang/zig/blob/d23db9427b82d7dc798442accf34729c16e92f2d/lib/std/bit_set.zig#L322
switched to using comptime unreachable and relying on compiler to generate compile errors.
Please wait on this until I finish https://github.com/ziglang/zig/commits/wrangle-writer-buffering branch
superseded by writergate https://github.com/ziglang/zig/pull/24329