Help getting pointer to fields of generic type

The code is

            const endian = .little;
            const fields = @typeInfo(Data).Struct.fields;

            fn dataFromBytes(allocator: std.mem.Allocator, reader: std.io.AnyReader) !Data {
                var data: Data = undefined;
                inline for (fields) |field| {
                    const val = @field(&data, field.name);
                    val.* = switch (@typeInfo(field.type)) {
                        .Int => try reader.readInt(field.type, endian),
                        .Float => @panic("TODO: implement"),
                        .Pointer => |p| b: {
                            std.debug.assert(p.size == .Slice);
                            const T = p.child;
                            const slice = try allocator.alloc(T, try reader.readInt(u32, endian));
                            for (slice) |*elem| elem.* = try reader.readInt(T, endian);
                            break :b slice;
                        },
                        .Bool => try reader.readByte() == @intFromBool(true),
                        else => unreachable,
                    };
                }
            }

The error I get is

src/main.zig:56:24: error: cannot dereference non-pointer type 'u32'
                    val.* = switch (@typeInfo(field.type)) {

How can I work around this?

I just had to take a pointer of the @field thingy:

const val = &@field(&data, field.name);

brainfart lmao…

1 Like