Skip to content
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

std.mem.bytesAsValue/asBytes for padded packed structs #10958

Open
motiejus opened this issue Feb 21, 2022 · 3 comments
Open

std.mem.bytesAsValue/asBytes for padded packed structs #10958

motiejus opened this issue Feb 21, 2022 · 3 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@motiejus
Copy link
Contributor

motiejus commented Feb 21, 2022

Currently std.mem.asBytes/bytesAsValue cannot be used for packed structs that "may be" padded. I.e. if 8*@sizeOf(T) != @bitSizeOf(T). In particular, this fails:

const std = @import("std");
const testing = std.testing;

test "bytesAsValue and asBytes work with non-word-aligned structs" {
    const Padded = packed struct { a: u12, b: u12 };
    const padded = Padded{ .a = 42, .b = 1 };
    const bytes = std.mem.asBytes(&padded);
    try testing.expectEqual(bytes.len, 3);
    const padded2 = std.mem.bytesAsValue(Padded, bytes[0..3]);
    try testing.expectEqual(padded, padded2.*);
}

With:

./code/zig-linux-x86_64-0.10.0-dev.865+8841a71aa/lib/std/mem.zig:2698:9: error: expected *[4]u8, passed *const [3]u8
        @compileError(std.fmt.bufPrint(&buf, "expected *[{}]u8, passed " ++ @typeName(B), .{size}) catch unreachable);
        ^
./code/zig-linux-x86_64-0.10.0-dev.865+8841a71aa/lib/std/mem.zig:2706:77: note: called from here
pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) {
                                                                            ^
./code/zig-linux-x86_64-0.10.0-dev.865+8841a71aa/lib/std/mem.zig:2706:77: note: called from here
pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) {
                                                                            ^
./test.zig:4:68: note: called from here
test "bytesAsValue and asBytes work with non-word-aligned structs" {

I've attempted to fix this in #10941 for bytesAsValue, but asBytes is more nuanced. Before spending much more time on this, I'd like to receive a "consensus" that this (bytesAsValue/asBytes) is something that ought to work for byte-aligned packed structs. If yes, how it should behave for non-byte aligned or padded packed structs?

@motiejus motiejus added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Feb 21, 2022
@ominitay
Copy link
Contributor

This is impossible. Regular structs don't have a fixed memory layout.

@motiejus motiejus changed the title std.mem.bytesAsValue/asBytes for padded structs std.mem.bytesAsValue/asBytes for padded packed structs Feb 21, 2022
@motiejus
Copy link
Contributor Author

This is impossible. Regular structs don't have a fixed memory layout.

Github failed me, you saw the stub issue before it was submitted. :)

@ominitay
Copy link
Contributor

Ahhh, apologies :)

@Vexu Vexu added this to the 0.10.0 milestone Mar 27, 2022
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants