| ... | @@ -556,6 +556,32 @@ test "std.meta.fieldInfo" { | ... | @@ -556,6 +556,32 @@ test "std.meta.fieldInfo" { |
| 556 | try testing.expect(comptime uf.type == u8); | 556 | try testing.expect(comptime uf.type == u8); |
| 557 | } | 557 | } |
| 558 | | 558 | |
| | 559 | pub fn FieldType(comptime T: type, comptime field: FieldEnum(T)) type { |
| | 560 | if (@typeInfo(T) != .Struct and @typeInfo(T) != .Union) { |
| | 561 | @compileError("Expected struct or union, found '" ++ @typeName(T) ++ "'"); |
| | 562 | } |
| | 563 | |
| | 564 | return fieldInfo(T, field).type; |
| | 565 | } |
| | 566 | |
| | 567 | test "std.meta.FieldType" { |
| | 568 | const S = struct { |
| | 569 | a: u8, |
| | 570 | b: u16, |
| | 571 | }; |
| | 572 | |
| | 573 | const U = union { |
| | 574 | c: u32, |
| | 575 | d: *const u8, |
| | 576 | }; |
| | 577 | |
| | 578 | try testing.expect(FieldType(S, .a) == u8); |
| | 579 | try testing.expect(FieldType(S, .b) == u16); |
| | 580 | |
| | 581 | try testing.expect(FieldType(U, .c) == u32); |
| | 582 | try testing.expect(FieldType(U, .d) == *const u8); |
| | 583 | } |
| | 584 | |
| 559 | pub fn fieldNames(comptime T: type) *const [fields(T).len][]const u8 { | 585 | pub fn fieldNames(comptime T: type) *const [fields(T).len][]const u8 { |
| 560 | comptime { | 586 | comptime { |
| 561 | const fieldInfos = fields(T); | 587 | const fieldInfos = fields(T); |