aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlexandros Naskos <alex_naskos@hotmail.com>2020-11-06 10:45:49 +0200
committerVeikka Tuominen <git@vexu.eu>2020-11-18 13:16:56 +0200
commit252924ae33cbdd65ef0bc5d878a2dad57fb1ada6 (patch)
tree698f94eb1d9d73e662150f45ae1f6208f9fedcdf /lib
parent6d5b76a75d204ac2ce9fb2e03744d9733169dbe3 (diff)
downloadzig-252924ae33cbdd65ef0bc5d878a2dad57fb1ada6.tar.gz
zig-252924ae33cbdd65ef0bc5d878a2dad57fb1ada6.zip
Added std.meta.fieldNames
Diffstat (limited to 'lib')
-rw-r--r--lib/std/meta.zig41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 8b0f9a2738..f1e9711b36 100644
--- 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,