feature. See also
. The project being documented here (as the example) is the Zig library itself.
Serializer.canSerializeTypeInner
fn canSerializeTypeInner(
T: type,
visited: []const type,
parent_is_optional: bool,
) bool
File
Code
fn canSerializeTypeInner(
T: type,
visited: []const type,
parent_is_optional: bool,
) bool {
return switch (@typeInfo(T)) {
.bool,
.int,
.float,
.comptime_float,
.comptime_int,
.null,
.enum_literal,
=> true,
.noreturn,
.void,
.type,
.undefined,
.error_union,
.error_set,
.@"fn",
.frame,
.@"anyframe",
.@"opaque",
.spirv,
=> false,
.@"enum" => |@"enum"| @"enum".mode == .exhaustive,
.pointer => |pointer| switch (pointer.size) {
.one => canSerializeTypeInner(pointer.child, visited, parent_is_optional),
.slice => canSerializeTypeInner(pointer.child, visited, false),
.many, .c => false,
},
.optional => |optional| if (parent_is_optional)
false
else
canSerializeTypeInner(optional.child, visited, true),
.array => |array| canSerializeTypeInner(array.child, visited, false),
.vector => |vector| canSerializeTypeInner(vector.child, visited, false),
.@"struct" => |@"struct"| {
for (visited) |V| if (T == V) return true;
const new_visited = visited ++ .{T};
for (@"struct".field_types) |field_type| {
if (!canSerializeTypeInner(field_type, new_visited, false)) return false;
}
return true;
},
.@"union" => |@"union"| {
for (visited) |V| if (T == V) return true;
const new_visited = visited ++ .{T};
if (@"union".tag_type == null) return false;
for (@"union".field_types) |field_type| {
if (field_type != void and !canSerializeTypeInner(field_type, new_visited, false)) {
return false;
}
}
return true;
},
};
}