DoxigAlpha

free

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.

Function parameters

Parameters

#
value:anytype

Configuration for the runtime parser.

Types

#
Options
Configuration for the runtime parser.
Diagnostics
Information about the success or failure of a parse.

Parses the given slice as ZON.

Functions

#
fromSlice
Parses the given slice as ZON.
fromZoir
Like `fromSlice`, but operates on `Zoir` instead of ZON source.
fromZoirNode
Like `fromZoir`, but the parse starts on `node` instead of root.
free
Frees ZON values.

Source

Implementation

#
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,
    }
}