Fully implement api free()
This commit is contained in:
parent
718da7b408
commit
73662d675a
1 changed files with 17 additions and 9 deletions
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue