authorgravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-05-27 04:00:38+05:00
committergravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-05-27 04:00:38+05:00
log78a1f6976d625d23142f68ac47558b9766874276
tree8fb8ea88e2a85b1ea02f7c6fa192dbefd14e91ff
parentb6e1670d2ba1e7724878ee2b7176077da5cb7c35

Add more traits


1 files changed, 66 insertions(+), 0 deletions(-)

lib/std/meta/trait.zig+66
......@@ -345,3 +345,69 @@ test "std.meta.trait.isContainer" {
345345 testing.expect(isContainer(TestEnum));
346346 testing.expect(!isContainer(u8));
347347}
348
349pub fn hasDecls(comptime T: type, comptime names: var) bool {
350 inline for (names) |name| {
351 if (!@hasDecl(T, name))
352 return false;
353 }
354 return true;
355}
356
357test "std.meta.trait.hasDecls" {
358 const TestStruct1 = struct {};
359 const TestStruct2 = struct {
360 pub var a: u32;
361 pub var b: u32;
362 c: bool,
363 pub fn useless() void {}
364 };
365
366 testing.expect(!hasDecls(TestStruct1, .{"a"}));
367 testing.expect(hasDecls(TestStruct2, .{"a", "b"}));
368 testing.expect(hasDecls(TestStruct2, .{"a", "b", "useless"}));
369 testing.expect(!hasDecls(TestStruct2, .{"a", "b", "c"}));
370}
371
372pub fn hasFields(comptime T: type, comptime names: var) bool {
373 inline for (names) |name| {
374 if (!@hasField(T, name))
375 return false;
376 }
377 return true;
378}
379
380test "std.meta.trait.hasFields" {
381 const TestStruct1 = struct {};
382 const TestStruct2 = struct {
383 a: u32,
384 b: u32,
385 c: bool,
386 pub fn useless() void {}
387 };
388
389 testing.expect(!hasFields(TestStruct1, .{"a"}));
390 testing.expect(hasFields(TestStruct2, .{"a", "b"}));
391 testing.expect(hasFields(TestStruct2, .{"a", "b", "c"}));
392 testing.expect(!hasFields(TestStruct2, .{"a", "b", "useless"}));
393}
394
395pub fn hasFunctions(comptime T: type, comptime names: var) bool {
396 inline for (names) |name| {
397 if (!hasFn(name)(T))
398 return false;
399 }
400 return true;
401}
402
403test "std.meta.trait.hasFunctions" {
404 const TestStruct1 = struct {};
405 const TestStruct2 = struct {
406 pub fn a() void {}
407 fn b() void {}
408 };
409
410 testing.expect(!hasFunctions(TestStruct1, .{"a"}));
411 testing.expect(hasFunctions(TestStruct2, .{"a", "b"}));
412 testing.expect(!hasFunctions(TestStruct2, .{"a", "b", "c"}));
413}