-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
disallow readStruct for packed structs #21562
Conversation
Here's an alternative that should handle (ignore) byte-sized padding as well, for discussion: pub fn readStruct(self: Self, comptime T: type) anyerror!T {
const size_bytes = @divExact(@bitSizeOf(T), 8);
//comptime assert(@sizeOf(T) == size_bytes); //this assert would reject types with byte-sized alignment padding, imo we could just as well support them
var res: [size_bytes]u8 = undefined;
try self.readNoEof(&res);
return @bitCast(res); //already results in a compile error for auto-layout structs, an explicit check isn't needed
} |
// Packed structs may have padding due to alignment of the backing integer. | ||
// Therefore, only extern structs are allowed. | ||
comptime assert(@typeInfo(T).@"struct".layout == .@"extern"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Packed structs may have padding due to alignment of the backing integer. | |
// Therefore, only extern structs are allowed. | |
comptime assert(@typeInfo(T).@"struct".layout == .@"extern"); | |
comptime assert(@typeInfo(T) == .@"struct"); | |
comptime assert(std.meta.hasUniqueRepresentation(T)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regular structs do not have a defined field order and the order may be changed by the compiler. This is not a sound suggestion as it will break when such happens.
If we want to support packed structs, two questions need to be answered:
I am happy to implement changes to support packed structs, or simply remove support here with this PR and open a separate issue on how to handle packed structs. |
|
Packed structs have the alignment of the backing integer. This means a packed struct with backing integer u24 will occupy 4 bytes of memory and the most significant 8 bits will be padding. |
The source of this issue is that the current definition uses |
Ah, this! As said, we should use Lines 279 to 282 in 0345775
|
Sounds like we want to support packed structs. I will open a separate PR for this soon (couple days). Closing. |
Intended to close #12960
I did a quick grep of this entire repo and every usage of
readStruct
is using anextern
struct.Warning: I think this is considered a "breaking" change?