Function free [src]
Frees ZON values.
Provided for convenience, you may also free these values on your own using the same allocator
passed into the parser.
Asserts at comptime that sufficient information is available via the type system to free this
value. Untagged unions, for example, will fail this assert.
Prototype
pub fn free(gpa: Allocator, value: anytype) void
Parameters
gpa: Allocator
Source
pub fn free(gpa: Allocator, value: anytype) void {
const Value = @TypeOf(value);
_ = valid_types;
switch (@typeInfo(Value)) {
.bool, .int, .float, .@"enum" => {},
.pointer => |pointer| {
switch (pointer.size) {
.one => {
free(gpa, value.*);
gpa.destroy(value);
},
.slice => {
for (value) |item| {
free(gpa, item);
}
gpa.free(value);
},
.many, .c => comptime unreachable,
}
},
.array => for (value) |item| {
free(gpa, item);
},
.@"struct" => |@"struct"| inline for (@"struct".fields) |field| {
free(gpa, @field(value, field.name));
},
.@"union" => |@"union"| if (@"union".tag_type == null) {
if (comptime requiresAllocator(Value)) unreachable;
} else switch (value) {
inline else => |_, tag| {
free(gpa, @field(value, @tagName(tag)));
},
},
.optional => if (value) |some| {
free(gpa, some);
},
.vector => |vector| for (0..vector.len) |i| free(gpa, value[i]),
.void => {},
else => comptime unreachable,
}
}