Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

FieldEnum

Returns an enum with a variant named after each field of T.

meta.FieldEnum
pub fn FieldEnum(comptime T: type) type

File

lib/std/meta.zig:396

Code

pub fn FieldEnum(comptime T: type) type {
    const field_names = fieldNames(T);

    switch (@typeInfo(T)) {
        .@"union" => |@"union"| if (@"union".tag_type) |EnumTag| {
            for (std.enums.values(EnumTag), 0..) |v, i| {
                if (@backingInt(v) != i) break; // enum values not consecutive
                if (!std.mem.eql(u8, @tagName(v), field_names[i])) break; // fields out of order
            } else {
                return EnumTag;
            }
        },
        else => {},
    }

    if (field_names.len == 0) return enum {};
    const IntTag = std.math.IntFittingRange(0, field_names.len - 1);
    return @Enum(IntTag, .exhaustive, field_names, &std.simd.iota(IntTag, field_names.len));
}