diff options
| author | daurnimator <quae@daurnimator.com> | 2020-12-13 22:12:28 +1100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-01-01 15:48:46 -0700 |
| commit | 9c97a07f18413fb0a535bc89ea9aecd1656cda6a (patch) | |
| tree | 95952ace758a0938eeed0347d366f71931e0e8c3 /lib/std/meta.zig | |
| parent | 73bf2e1525a392d367525ef96196fe15b14d8c30 (diff) | |
| download | zig-9c97a07f18413fb0a535bc89ea9aecd1656cda6a.tar.gz zig-9c97a07f18413fb0a535bc89ea9aecd1656cda6a.zip | |
std: have std.meta.fieldInfo take an enum rather than a string
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 1dac55dbb3..2c94569bf1 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -487,19 +487,14 @@ test "std.meta.fields" { testing.expect(comptime uf[0].field_type == u8); } -pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) { +pub fn fieldInfo(comptime T: type, comptime field: FieldEnum(T)) switch (@typeInfo(T)) { .Struct => TypeInfo.StructField, .Union => TypeInfo.UnionField, .ErrorSet => TypeInfo.Error, .Enum => TypeInfo.EnumField, else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), } { - inline for (comptime fields(T)) |field| { - if (comptime mem.eql(u8, field.name, field_name)) - return field; - } - - @compileError("'" ++ @typeName(T) ++ "' has no field '" ++ field_name ++ "'"); + return fields(T)[@enumToInt(field)]; } test "std.meta.fieldInfo" { @@ -514,10 +509,10 @@ test "std.meta.fieldInfo" { a: u8, }; - const e1f = comptime fieldInfo(E1, "A"); - const e2f = comptime fieldInfo(E2, "A"); - const sf = comptime fieldInfo(S1, "a"); - const uf = comptime fieldInfo(U1, "a"); + const e1f = fieldInfo(E1, .A); + const e2f = fieldInfo(E2, .A); + const sf = fieldInfo(S1, .a); + const uf = fieldInfo(U1, .a); testing.expect(mem.eql(u8, e1f.name, "A")); testing.expect(mem.eql(u8, e2f.name, "A")); |
