diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-10-16 01:29:16 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-10-16 01:49:02 -0400 |
| commit | 1014cfdf3b60faf9af5b062d198f4f44976cb1bc (patch) | |
| tree | cfa5bff2c520b70c510e3a6ca44b35588417be38 /lib/std/meta.zig | |
| parent | 47dfaf3d175ebea63b169a373b4dec4af6af7001 (diff) | |
| download | zig-1014cfdf3b60faf9af5b062d198f4f44976cb1bc.tar.gz zig-1014cfdf3b60faf9af5b062d198f4f44976cb1bc.zip | |
generated docs: progress towards generic types being useful
See #3406
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 52d8b54ecc..46018e0b1d 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -542,3 +542,13 @@ pub fn intToEnum(comptime Tag: type, tag_int: var) IntToEnumError!Tag { } return error.InvalidEnumTag; } + +/// Given a type and a name, return the field index according to source order. +/// Returns `null` if the field is not found. +pub fn fieldIndex(comptime T: type, comptime name: []const u8) ?comptime_int { + inline for (fields(T)) |field, i| { + if (mem.eql(u8, field.name, name)) + return comptime_int(i); + } + return null; +} |
