The inner one I believe should be packed struct because it is for bit packing (but maybe, my understanding may be wrong). At first, I thought the outer one can be extern struct, however there is one field that is u48 which is I also believe not byte-aligned types. Then, I concluded that both the inner and outer struct should be packed struct. That is my thought process, if something is wrong, I will be glad to be corrected.
To give you the full use case, here is the actual struct that I use. This struct is being used to send and receive information from PC to microcontroller (written in C). As a note, currently this struct is a legacy code so I currently do not have the abiility to change the struct.
const std = @import("std");
const MyType = packed struct(u64) {
a: f32,
b: u10,
c: u6,
d: bool,
e: bool,
f: bool,
g: bool,
h: packed struct {
h1: bool,
h2: bool,
},
i: bool,
j: u1,
k: enum(u4) { dummy },
l: u4,
};
const MyOuterType = packed struct(u256) {
a: enum(u16) { dummy },
b: u48,
c: packed struct(u192) {
c1: MyType,
c2: MyType,
c3: MyType,
},
};
pub fn main() !void {
const my_type_variable = std.mem.zeroInit(MyType, .{
.d = true,
.e = true,
.f = true,
});
var my_outer_variable = std.mem.zeroInit(MyOuterType, .{});
my_outer_variable.c.c1 = my_type_variable;
const my_pointer: *MyType = @ptrCast(&my_outer_variable.c.c1);
std.log.debug("pointer value: {}", .{my_pointer.*});
std.log.debug("actual value: {}", .{my_outer_variable.c.c1});
}
Currently, even this snippet already give the wrong output
debug: pointer value: .{ .a = 0, .b = 0, .c = 0, .d = false, .e = false, .f = false, .g = false, .h = .{ .h1 = false, .h2 = false }, .i = false, .j = 0, .k = .dummy, .l = 0 }
debug: actual value: .{ .a = 0, .b = 0, .c = 0, .d = true, .e = true, .f = true, .g = false, .h = .{ .h1 = false, .h2 = false }, .i = false, .j = 0, .k = .dummy, .l = 0 }