Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn hasFieldRec(comptime name: []const u8) TraitFn {
- const Closure = struct {
- pub fn trait(comptime T: type) bool {
- const info = @typeInfo(T);
- const fields = switch (info) {
- builtin.TypeId.Struct => |s| s.fields,
- builtin.TypeId.Union => |u| u.fields,
- builtin.TypeId.Enum => |e| e.fields,
- else => return false,
- };
- inline for(fields) |field| {
- if (mem.eql(u8, field.name, name)) return true;
- }
- inline for(fields) |field| {
- if (hasFieldRec(name)(field.field_type)) return true;
- }
- return false;
- }
- };
- return Closure.trait;
- }
- test "rec field" {
- const base = struct {
- number: i32,
- };
- const nest = struct {
- nesting: base,
- };
- std.debug.assert(hasFieldRec("number")(nest));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement