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.
pub fn free(gpa: Allocator, value: anytype) void
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 => {
freeArray(gpa, @TypeOf(value), &value);
},
.vector => |vector| {
const array: [vector.len]vector.child = value;
freeArray(gpa, @TypeOf(array), &array);
},
.@"struct" => |@"struct"| inline for (@"struct".field_names) |field_name| {
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);
},
.void => {},
else => comptime unreachable,
}
}