commit 252924ae33cbdd65ef0bc5d878a2dad57fb1ada6 (tree)
parent 6d5b76a75d204ac2ce9fb2e03744d9733169dbe3
Author: Alexandros Naskos <alex_naskos@hotmail.com>
Date: Fri, 6 Nov 2020 10:45:49 +0200
Added std.meta.fieldNames
Diffstat:
1 file changed, 41 insertions(+), 0 deletions(-)
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
@@ -413,6 +413,47 @@ test "std.meta.fieldInfo" {
testing.expect(comptime uf.field_type == u8);
}
+pub fn fieldNames(comptime T: type) *const [fields(T).len][]const u8 {
+ comptime {
+ const fieldInfos = fields(T);
+ var names: [fieldInfos.len][]const u8 = undefined;
+ for (fieldInfos) |field, i| {
+ names[i] = field.name;
+ }
+ return &names;
+ }
+}
+
+test "std.meta.fieldNames" {
+ const E1 = enum {
+ A, B
+ };
+ const E2 = error{A};
+ const S1 = struct {
+ a: u8,
+ };
+ const U1 = union {
+ a: u8,
+ b: void,
+ };
+
+ const e1names = fieldNames(E1);
+ const e2names = fieldNames(E2);
+ const s1names = fieldNames(S1);
+ const u1names = fieldNames(U1);
+
+ testing.expect(e1names.len == 2);
+ testing.expectEqualSlices(u8, e1names[0], "A");
+ testing.expectEqualSlices(u8, e1names[1], "B");
+ testing.expect(e2names.len == 1);
+ testing.expectEqualSlices(u8, e2names[0], "A");
+ testing.expect(s1names.len == 1);
+ testing.expectEqualSlices(u8, s1names[0], "a");
+ testing.expect(u1names.len == 2);
+ testing.expectEqualSlices(u8, u1names[0], "a");
+ testing.expectEqualSlices(u8, u1names[1], "b");
+}
+
pub fn TagType(comptime T: type) type {
return switch (@typeInfo(T)) {
.Enum => |info| info.tag_type,