std: add meta.FieldType

This is simply a small convenience wrapper around
'meta.fieldInfo(T, .field).type'. However, this operation is common
enough that it makes sense to have its own function for.
This commit is contained in:
mlugg
2022-12-21 12:00:55 +00:00
committed by Veikka Tuominen
parent 5bd69c655d
commit 8094fa5d48

View File

@@ -556,6 +556,32 @@ test "std.meta.fieldInfo" {
try testing.expect(comptime uf.type == u8);
}
pub fn FieldType(comptime T: type, comptime field: FieldEnum(T)) type {
if (@typeInfo(T) != .Struct and @typeInfo(T) != .Union) {
@compileError("Expected struct or union, found '" ++ @typeName(T) ++ "'");
}
return fieldInfo(T, field).type;
}
test "std.meta.FieldType" {
const S = struct {
a: u8,
b: u16,
};
const U = union {
c: u32,
d: *const u8,
};
try testing.expect(FieldType(S, .a) == u8);
try testing.expect(FieldType(S, .b) == u16);
try testing.expect(FieldType(U, .c) == u32);
try testing.expect(FieldType(U, .d) == *const u8);
}
pub fn fieldNames(comptime T: type) *const [fields(T).len][]const u8 {
comptime {
const fieldInfos = fields(T);