marlersoft / zigwin32gen

Generates Complete Zig bindings for Win32. See https://github.com/marlersoft/zigwin32 for the bindings themselves.
108 stars 17 forks source link

Support PackingSize > 1 #2

Closed marler8997 closed 3 years ago

marler8997 commented 3 years ago

A "PackingSize" of 0 is equivalent to an extern struct/union in Zig, and a PackingSize of 1 is equivalent to a packed struct/union in Zig. However, Zig doesn't have a way to modify a struct to emulate packing sizes larger than 1.

My current idea to support this, is when the packing size is greater than 1, I can use a packed struct, and then add padding between each field based on the size of the previous field, i.e.

// emulate PackingSize of 2
const S = packed struct {
    foo: T,
    _padding0_: PaddingFor(T, 2),
    bar: U,
    _padding1_: PaddingFor(U, 2),
};

// emulate PackingSize of 4
const S = packed struct {
    foo: T,
    _padding0_: PaddingFor(T, 4),
    bar: U,
    _padding1_: PaddingFor(U, 4),
};
marler8997 commented 3 years ago

Nevermind, thanks to daurnimator, he pointed out zig already supports field alignment.

// emulate PackingSize of 2
const S = packed struct {
    foo: T align(2),
    bar: U align(2),
};

Fixed in https://github.com/marlersoft/zigwin32/commit/e6c6b6c5ad720bb08c92e01750fee4423419d7c5