I’ve been trying to create a system where i can store different types of data as “bundles”. The input order should not matter (i sort the input by their type), and i should be able to get only parts of the data.
I basically want it to work like this
test "test" {
const allocator = testing.allocator;
const Struct1 = struct { x: u32, y: u32 };
const Struct2 = struct { x: u16 };
const Struct3 = struct { x: u64 };
const BundleTypes1 = .{ Struct1, Struct3, Struct2 };
var bundles = try Bundles.init(BundleTypes1, allocator);
defer bundles.deinit();
bundles.add(.{ Struct3{ .x = 64 }, Struct1{ .x = 32, .y = 32 }, Struct2{ .x = 16 }});
_ = bundles.get(.{ Struct1 Struct3 }).iterator();
}
I’ve got it to mostly working. I sort the types and create some meta data about the offset and size of each type contained in the bundle type when i create the bundle. I can add a bundle with the types in any order. The problem comes when i want to get the data. The metadata is runtime and the input types is comptime so i can’t sort the types based on metadata to later cast the output(the bundle is stored as []u8 in memory). I also might not have all the types when getting the data and can therefore not re-generate the metadata.
Making the metadata comptime would solve the issue. I’ve tried to use a vtable to do this and this enables me to get access to the metadata at compiletime.
pub const VTable = struct {
add: *const fn (usize, *Bundles, usize) void,
};
pub fn init(type_bundle: anytype, allocator: Allocator) !Bundles {
const ObjectType = Object(type_bundle);
const BundleType = @TypeOf(type_bundle);
const object = try allocator.create(ObjectType);
errdefer allocator.destroy(object);
const bundles = Bundles{
.allocator = allocator,
.object = @intFromPtr(object),
.vtable = &comptime VTable{
.add = struct {
fn add(ptr: usize, ctx: *Bundles, bundle_ptr: usize) void {
// I no longer have access to the anytype of the bundle
// Since you cant have anytype in the vtable
const self: *ObjectType = @ptrFromInt(ptr);
const bundle: *BundleType = @ptrFromInt(bundle_ptr);
@call(std.builtin.CallModifier.always_inline, ObjectType.add, .{ self, ctx, bundle.* });
}
}.add,
},
};
object.* = try ObjectType.init();
return bundles;
}
pub fn BundleObject(bundle_types: anytype) type {
const meta = GenerateBundleMeta(bundle_types, bundle_types.len, PackingStrategy.Aligned);
return struct {
const Self = @This();
const BundleType: type = @TypeOf(bundle_types);
const ObjectMeta: @TypeOf(meta) = meta;
fn add(self: *Self, ctx: *Bundles, bundle: anytype) void {}
};
}
This creates some other problems where i no longer have access to the “anytype” of the bundle.
So i’ve in desperation tried to do stuff like this as well:
vtable: *const GenerateVTable(VTableFunctions),
pub fn GenerateVTable(comptime functions: type) type {
const BuiltinType = std.builtin.Type;
const table_info = @typeInfo(functions);
comptime var tfields: [table_info.Struct.fields.len]BuiltinType.StructField = undefined;
for (table_info.Struct.fields, 0..) |field, ii| {
if (!field.is_comptime) @compileError("Function needs to be comptime");
tfields[ii] = BuiltinType.StructField{
.name = field.name,
.alignment = field.alignment,
.type = field.type,
.is_comptime = field.is_comptime,
.default_value = field.default_value,
};
}
const decls: [0]BuiltinType.Declaration = undefined;
return @Type(.{
.Struct = .{
.layout = BuiltinType.ContainerLayout.auto,
.backing_integer = null,
.fields = &tfields,
.decls = &decls,
.is_tuple = false,
},
});
}
const VTableFunctions = struct {
comptime add: fn (*Bundles, anytype) void = struct {
pub fn add(self: *Bundles, bundle: anytype) void {
std.debug.print("Me add: {}n", .{bundle});
_ = self;
// self.object.add(bundle); <-- no type to cast to
}
}.add,
};
pub fn init(type_bundle: anytype, allocator: Allocator) !Bundles {
// ..
const bundles = Bundles{
//..
.vtable = &comptime GenerateVTable(VTableFunctions){},
}
return bundles;
}
Now i have access to the bundles “anytype” but i can no longer generate and store the metadata at comptime.
So i thought i might be able to do this:
pub fn init(type_bundle: anytype, allocator: Allocator) !Bundles {
// ..
const me = Bundles {
//..
.vtable = &comptime GenerateVTable(VTableFunctions){
.add = struct {
pub fn add(self: *Bundles, Entity, bundle: anytype) void {
// ..
}
}.add,
},
}
// ..
}
But this gives me this error: error: value stored in comptime field does not match the default value of the field
. I kind of knew this already, but yeah… desperation.
I’m kind of new to the c-style of programming and since zig does not have traits or interfaces this kind of problem is kind of new to me.
One of the easiest solutions would maybe be to make Bundles a genereic but then I’ll just move the complexity to other locations. For example how do i store hundreds of different bundles when each bundle is it’s own type (Bundle(.{Struct 1}) vs Bundle(.{Struct2)
? Nah i don’t want this.
What Am i missing here, what can i do to archive my goal?
Thanks in advance for any help you can provide.