zig icon indicating copy to clipboard operation
zig copied to clipboard

Support packed structs in std.io.Reader and std.io.Writer

Open jeffective opened this issue 1 year ago • 4 comments

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:

  1. 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.
  2. I chose to restrict packed struct parameters to having a @bitSizeOf that 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.
  3. For endianness conversion, I chose to use std.mem.reverse instead 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!

jeffective avatar Oct 05 '24 05:10 jeffective

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

jeffective avatar Oct 05 '24 19:10 jeffective

@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.

jeffective avatar Oct 06 '24 18:10 jeffective

@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.

rootbeer avatar Oct 06 '24 19:10 rootbeer

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.

jeffective avatar Oct 14 '24 00:10 jeffective

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

switched to using comptime unreachable and relying on compiler to generate compile errors.

jeffective avatar Jan 31 '25 05:01 jeffective

Please wait on this until I finish https://github.com/ziglang/zig/commits/wrangle-writer-buffering branch

andrewrk avatar Apr 11 '25 20:04 andrewrk

superseded by writergate https://github.com/ziglang/zig/pull/24329

jeffective avatar Jul 15 '25 00:07 jeffective