Crude example:
const std = @import("std");
const Foo = struct {};
const Bar = struct {};
pub const Entity = union(enum) {
foo: Foo,
bor: Bar,
};
fn add(map: *std.AutoArrayHashMap(u32, Entity), e: anytype) !void {
map.put(0, .{ .foo = e });
}
pub fn main() !void {
var debug = std.heap.DebugAllocator(.{}){};
defer _ = debug.deinit();
const alloc = debug.allocator();
var entities: std.AutoArrayHashMap(u32, Entity) = .init(alloc);
defer entities.deinit();
const entity: Foo = .{};
try add(&entities, entity);
}
I'd like to adapt the add function to take anytype (as a Foo or Bar), and have the function create the respective wrapping of Entity.
I want to do this dynamically, instead of having a switch statement inside add.
Can this be done? I could iterate over Entity to find the field of the matching type, but am not sure how I could generate .{ .foo = e} for example.