I'm working on an OpenGL wrapper, and I have a generic buffer struct like this:
pub fn Buffer(comptime T: type) type {
return struct {
// fields
// methods
pub fn writeVertexData(self: *Self, vertices: []const T, offset: u32) !void {
// ...
}
};
}
I'm trying to avoid having anytype on the Buffer methods and then having to handle reflection paths.
The issue is, when I try to reference these Buffers in other structs, I'm not able to since they don't actually exist until created with a specific type. So this doesn't work:
const BufferBinding = struct {
buffer: *Buffer,
// other fields
};
What I want to do is to be able to specify that the buffer field in the above BufferBinding struct is generated from my Buffer() function (the way generic base types can be referenced in other languages). I've tried working around this by creating an opaque "AnyBuffer" constant, but that didn't work either (still getting a handle on how to use opaques).
Feels like there should be a common pattern for handling this sort of thing, but I'm having trouble finding any examples. I feel like it's going to be super obvious once someone points it out to me.
Any thoughts on the best way to handle this?