Fully implement api free()

This commit is contained in:
jaina heartles 2022-07-19 02:22:19 -07:00
parent 718da7b408
commit 73662d675a

View file

@ -9,15 +9,23 @@ pub const Uuid = util.Uuid;
// Frees an api struct and its fields allocated from alloc // Frees an api struct and its fields allocated from alloc
pub fn free(alloc: std.mem.Allocator, val: anytype) void { pub fn free(alloc: std.mem.Allocator, val: anytype) void {
inline for (std.meta.fields(@TypeOf(val))) |f| { switch (@typeInfo(@TypeOf(val))) {
// TODO .Pointer => |ptr_info| switch (ptr_info.size) {
if (f.field_type == []u8 or f.field_type == []const u8) { .One => {
alloc.free(@field(val, f.name)); free(alloc, val.*);
} else if (f.field_type == Uuid or f.field_type == DateTime) { alloc.destroy(val);
// nothing },
} else { .Slice => {
@compileError("unsupported field type " ++ @typeName(f.field_type)); for (val) |elem| free(alloc, elem);
} alloc.free(val);
},
else => unreachable,
},
.Struct => inline for (std.meta.fields(@TypeOf(val))) |f| free(alloc, @field(val, f.name)),
.Array => for (val) |elem| free(alloc, elem),
.Optional => if (val) |opt| free(alloc, opt),
.Bool, .Int, .Float, .Enum => {},
else => unreachable,
} }
} }