I've been stuck on this for a minute.
I have an entity struct that's similar to
struct {
inner: *anyopaque,
alignment: u8, // power of 2
pub fn init(data: anytype) @This() {
const data_ptr = alloc.create(@TypeOf(data)) catch @panic("oom"); // create space for a copy
@memcpy(std.mem.asBytes(data_ptr), std.mem.asBytes(&data)); // copies data into data_ptr
return @This() {
inner = @ptrcast(data_ptr), // typed* -> opaque*
alignment: @alignOf(@TypeOf(data)), // power of 2
}
}
pub fn deinit(this: *@This()) void {
// ?
}
}
I alloc.create, but how do I alloc.destroy from the *anyopaque? The documentation for alloc.destroy mentions it only needs a ptr and the alignment but I can't figure out how to make the needed aligned pointer. std.mem.alignPointer() didn't work out for me because it requires a many item pointer
Any help would be appreciated :)