| ... | @@ -105,6 +105,7 @@ test "std.meta.isTag for Tagged Unions" { | ... | @@ -105,6 +105,7 @@ test "std.meta.isTag for Tagged Unions" { |
| 105 | try testing.expect(!isTag(flt, "int")); | 105 | try testing.expect(!isTag(flt, "int")); |
| 106 | } | 106 | } |
| 107 | | 107 | |
| | 108 | /// Returns the variant of an enum type, `T`, which is named `str`, or `null` if no such variant exists. |
| 108 | pub fn stringToEnum(comptime T: type, str: []const u8) ?T { | 109 | pub fn stringToEnum(comptime T: type, str: []const u8) ?T { |
| 109 | // Using ComptimeStringMap here is more performant, but it will start to take too | 110 | // Using ComptimeStringMap here is more performant, but it will start to take too |
| 110 | // long to compile if the enum is large enough, due to the current limits of comptime | 111 | // long to compile if the enum is large enough, due to the current limits of comptime |
| ... | @@ -192,6 +193,7 @@ test "std.meta.alignment" { | ... | @@ -192,6 +193,7 @@ test "std.meta.alignment" { |
| 192 | try testing.expect(alignment(fn () align(128) void) == 128); | 193 | try testing.expect(alignment(fn () align(128) void) == 128); |
| 193 | } | 194 | } |
| 194 | | 195 | |
| | 196 | /// Given a parameterized type (array, vector, pointer, optional), returns the "child type". |
| 195 | pub fn Child(comptime T: type) type { | 197 | pub fn Child(comptime T: type) type { |
| 196 | return switch (@typeInfo(T)) { | 198 | return switch (@typeInfo(T)) { |
| 197 | .Array => |info| info.child, | 199 | .Array => |info| info.child, |
| ... | @@ -210,7 +212,7 @@ test "std.meta.Child" { | ... | @@ -210,7 +212,7 @@ test "std.meta.Child" { |
| 210 | try testing.expect(Child(Vector(2, u8)) == u8); | 212 | try testing.expect(Child(Vector(2, u8)) == u8); |
| 211 | } | 213 | } |
| 212 | | 214 | |
| 213 | /// Given a "memory span" type, returns the "element type". | 215 | /// Given a "memory span" type (array, slice, vector, or pointer to such), returns the "element type". |
| 214 | pub fn Elem(comptime T: type) type { | 216 | pub fn Elem(comptime T: type) type { |
| 215 | switch (@typeInfo(T)) { | 217 | switch (@typeInfo(T)) { |
| 216 | .Array => |info| return info.child, | 218 | .Array => |info| return info.child, |
| ... | @@ -505,7 +507,6 @@ test "std.meta.declarationInfo" { | ... | @@ -505,7 +507,6 @@ test "std.meta.declarationInfo" { |
| 505 | try testing.expect(!info.is_pub); | 507 | try testing.expect(!info.is_pub); |
| 506 | } | 508 | } |
| 507 | } | 509 | } |
| 508 | | | |
| 509 | pub fn fields(comptime T: type) switch (@typeInfo(T)) { | 510 | pub fn fields(comptime T: type) switch (@typeInfo(T)) { |
| 510 | .Struct => []const Type.StructField, | 511 | .Struct => []const Type.StructField, |
| 511 | .Union => []const Type.UnionField, | 512 | .Union => []const Type.UnionField, |
| ... | @@ -652,6 +653,7 @@ test "std.meta.tags" { | ... | @@ -652,6 +653,7 @@ test "std.meta.tags" { |
| 652 | try testing.expectEqual(E2.A, e2_tags[0]); | 653 | try testing.expectEqual(E2.A, e2_tags[0]); |
| 653 | } | 654 | } |
| 654 | | 655 | |
| | 656 | /// Returns an enum with a variant named after each field of `T`. |
| 655 | pub fn FieldEnum(comptime T: type) type { | 657 | pub fn FieldEnum(comptime T: type) type { |
| 656 | const field_infos = fields(T); | 658 | const field_infos = fields(T); |
| 657 | | 659 | |