| ... | ... | @@ -413,6 +413,47 @@ test "std.meta.fieldInfo" { |
| 413 | 413 | testing.expect(comptime uf.field_type == u8); |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | pub fn fieldNames(comptime T: type) *const [fields(T).len][]const u8 { |
| 417 | comptime { |
| 418 | const fieldInfos = fields(T); |
| 419 | var names: [fieldInfos.len][]const u8 = undefined; |
| 420 | for (fieldInfos) |field, i| { |
| 421 | names[i] = field.name; |
| 422 | } |
| 423 | return &names; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | test "std.meta.fieldNames" { |
| 428 | const E1 = enum { |
| 429 | A, B |
| 430 | }; |
| 431 | const E2 = error{A}; |
| 432 | const S1 = struct { |
| 433 | a: u8, |
| 434 | }; |
| 435 | const U1 = union { |
| 436 | a: u8, |
| 437 | b: void, |
| 438 | }; |
| 439 | |
| 440 | const e1names = fieldNames(E1); |
| 441 | const e2names = fieldNames(E2); |
| 442 | const s1names = fieldNames(S1); |
| 443 | const u1names = fieldNames(U1); |
| 444 | |
| 445 | testing.expect(e1names.len == 2); |
| 446 | testing.expectEqualSlices(u8, e1names[0], "A"); |
| 447 | testing.expectEqualSlices(u8, e1names[1], "B"); |
| 448 | testing.expect(e2names.len == 1); |
| 449 | testing.expectEqualSlices(u8, e2names[0], "A"); |
| 450 | testing.expect(s1names.len == 1); |
| 451 | testing.expectEqualSlices(u8, s1names[0], "a"); |
| 452 | testing.expect(u1names.len == 2); |
| 453 | testing.expectEqualSlices(u8, u1names[0], "a"); |
| 454 | testing.expectEqualSlices(u8, u1names[1], "b"); |
| 455 | } |
| 456 | |
| 416 | 457 | pub fn TagType(comptime T: type) type { |
| 417 | 458 | return switch (@typeInfo(T)) { |
| 418 | 459 | .Enum => |info| info.tag_type, |