From 78a1f6976d625d23142f68ac47558b9766874276 Mon Sep 17 00:00:00 2001 From: data-man Date: Wed, 27 May 2020 04:00:38 +0500 Subject: [PATCH 1/3] Add more traits --- lib/std/meta/trait.zig | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig index 3be13057b5..e3052f2a3a 100644 --- a/lib/std/meta/trait.zig +++ b/lib/std/meta/trait.zig @@ -345,3 +345,69 @@ test "std.meta.trait.isContainer" { testing.expect(isContainer(TestEnum)); testing.expect(!isContainer(u8)); } + +pub fn hasDecls(comptime T: type, comptime names: var) bool { + inline for (names) |name| { + if (!@hasDecl(T, name)) + return false; + } + return true; +} + +test "std.meta.trait.hasDecls" { + const TestStruct1 = struct {}; + const TestStruct2 = struct { + pub var a: u32; + pub var b: u32; + c: bool, + pub fn useless() void {} + }; + + testing.expect(!hasDecls(TestStruct1, .{"a"})); + testing.expect(hasDecls(TestStruct2, .{"a", "b"})); + testing.expect(hasDecls(TestStruct2, .{"a", "b", "useless"})); + testing.expect(!hasDecls(TestStruct2, .{"a", "b", "c"})); +} + +pub fn hasFields(comptime T: type, comptime names: var) bool { + inline for (names) |name| { + if (!@hasField(T, name)) + return false; + } + return true; +} + +test "std.meta.trait.hasFields" { + const TestStruct1 = struct {}; + const TestStruct2 = struct { + a: u32, + b: u32, + c: bool, + pub fn useless() void {} + }; + + testing.expect(!hasFields(TestStruct1, .{"a"})); + testing.expect(hasFields(TestStruct2, .{"a", "b"})); + testing.expect(hasFields(TestStruct2, .{"a", "b", "c"})); + testing.expect(!hasFields(TestStruct2, .{"a", "b", "useless"})); +} + +pub fn hasFunctions(comptime T: type, comptime names: var) bool { + inline for (names) |name| { + if (!hasFn(name)(T)) + return false; + } + return true; +} + +test "std.meta.trait.hasFunctions" { + const TestStruct1 = struct {}; + const TestStruct2 = struct { + pub fn a() void {} + fn b() void {} + }; + + testing.expect(!hasFunctions(TestStruct1, .{"a"})); + testing.expect(hasFunctions(TestStruct2, .{"a", "b"})); + testing.expect(!hasFunctions(TestStruct2, .{"a", "b", "c"})); +} From f9bdf325d318347547b3ea7735367b56b4a26cd2 Mon Sep 17 00:00:00 2001 From: data-man Date: Thu, 28 May 2020 16:39:00 +0500 Subject: [PATCH 2/3] Added tests with tuple --- lib/std/meta/trait.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig index e3052f2a3a..89cf02bcbd 100644 --- a/lib/std/meta/trait.zig +++ b/lib/std/meta/trait.zig @@ -363,10 +363,13 @@ test "std.meta.trait.hasDecls" { pub fn useless() void {} }; + const tuple = .{"a", "b", "c"}; + testing.expect(!hasDecls(TestStruct1, .{"a"})); testing.expect(hasDecls(TestStruct2, .{"a", "b"})); testing.expect(hasDecls(TestStruct2, .{"a", "b", "useless"})); testing.expect(!hasDecls(TestStruct2, .{"a", "b", "c"})); + testing.expect(!hasDecls(TestStruct2, tuple)); } pub fn hasFields(comptime T: type, comptime names: var) bool { @@ -386,9 +389,13 @@ test "std.meta.trait.hasFields" { pub fn useless() void {} }; + + const tuple = .{"a", "b", "c"}; + testing.expect(!hasFields(TestStruct1, .{"a"})); testing.expect(hasFields(TestStruct2, .{"a", "b"})); testing.expect(hasFields(TestStruct2, .{"a", "b", "c"})); + testing.expect(hasFields(TestStruct2, tuple)); testing.expect(!hasFields(TestStruct2, .{"a", "b", "useless"})); } @@ -407,7 +414,10 @@ test "std.meta.trait.hasFunctions" { fn b() void {} }; + const tuple = .{"a", "b", "c"}; + testing.expect(!hasFunctions(TestStruct1, .{"a"})); testing.expect(hasFunctions(TestStruct2, .{"a", "b"})); testing.expect(!hasFunctions(TestStruct2, .{"a", "b", "c"})); + testing.expect(!hasFunctions(TestStruct2, tuple)); } From c91786caf3f12ec9c9ad928fc0e71d43783ea7a8 Mon Sep 17 00:00:00 2001 From: data-man Date: Thu, 28 May 2020 16:41:15 +0500 Subject: [PATCH 3/3] zig fmt --- lib/std/meta/trait.zig | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig index 89cf02bcbd..3fe5152aac 100644 --- a/lib/std/meta/trait.zig +++ b/lib/std/meta/trait.zig @@ -363,12 +363,12 @@ test "std.meta.trait.hasDecls" { pub fn useless() void {} }; - const tuple = .{"a", "b", "c"}; + const tuple = .{ "a", "b", "c" }; testing.expect(!hasDecls(TestStruct1, .{"a"})); - testing.expect(hasDecls(TestStruct2, .{"a", "b"})); - testing.expect(hasDecls(TestStruct2, .{"a", "b", "useless"})); - testing.expect(!hasDecls(TestStruct2, .{"a", "b", "c"})); + testing.expect(hasDecls(TestStruct2, .{ "a", "b" })); + testing.expect(hasDecls(TestStruct2, .{ "a", "b", "useless" })); + testing.expect(!hasDecls(TestStruct2, .{ "a", "b", "c" })); testing.expect(!hasDecls(TestStruct2, tuple)); } @@ -389,14 +389,13 @@ test "std.meta.trait.hasFields" { pub fn useless() void {} }; - - const tuple = .{"a", "b", "c"}; + const tuple = .{ "a", "b", "c" }; testing.expect(!hasFields(TestStruct1, .{"a"})); - testing.expect(hasFields(TestStruct2, .{"a", "b"})); - testing.expect(hasFields(TestStruct2, .{"a", "b", "c"})); + testing.expect(hasFields(TestStruct2, .{ "a", "b" })); + testing.expect(hasFields(TestStruct2, .{ "a", "b", "c" })); testing.expect(hasFields(TestStruct2, tuple)); - testing.expect(!hasFields(TestStruct2, .{"a", "b", "useless"})); + testing.expect(!hasFields(TestStruct2, .{ "a", "b", "useless" })); } pub fn hasFunctions(comptime T: type, comptime names: var) bool { @@ -414,10 +413,10 @@ test "std.meta.trait.hasFunctions" { fn b() void {} }; - const tuple = .{"a", "b", "c"}; + const tuple = .{ "a", "b", "c" }; testing.expect(!hasFunctions(TestStruct1, .{"a"})); - testing.expect(hasFunctions(TestStruct2, .{"a", "b"})); - testing.expect(!hasFunctions(TestStruct2, .{"a", "b", "c"})); + testing.expect(hasFunctions(TestStruct2, .{ "a", "b" })); + testing.expect(!hasFunctions(TestStruct2, .{ "a", "b", "c" })); testing.expect(!hasFunctions(TestStruct2, tuple)); }