Type Function DeclEnum [src]
Prototype
pub fn DeclEnum(comptime T: type) type
Example
test DeclEnum {
const A = struct {
pub const a: u8 = 0;
};
const B = union {
foo: void,
pub const a: u8 = 0;
pub const b: void = {};
pub const c: f32 = 0;
};
const C = enum {
bar,
pub const a: u8 = 0;
pub const b: void = {};
pub const c: f32 = 0;
};
const D = struct {};
try expectEqualEnum(enum { a }, DeclEnum(A));
try expectEqualEnum(enum { a, b, c }, DeclEnum(B));
try expectEqualEnum(enum { a, b, c }, DeclEnum(C));
try expectEqualEnum(enum {}, DeclEnum(D));
}
Source
pub fn DeclEnum(comptime T: type) type {
const fieldInfos = std.meta.declarations(T);
var enumDecls: [fieldInfos.len]std.builtin.Type.EnumField = undefined;
var decls = [_]std.builtin.Type.Declaration{};
inline for (fieldInfos, 0..) |field, i| {
enumDecls[i] = .{ .name = field.name ++ "", .value = i };
}
return @Type(.{
.@"enum" = .{
.tag_type = std.math.IntFittingRange(0, if (fieldInfos.len == 0) 0 else fieldInfos.len - 1),
.fields = &enumDecls,
.decls = &decls,
.is_exhaustive = true,
},
});
}