| ... | @@ -345,3 +345,69 @@ test "std.meta.trait.isContainer" { | ... | @@ -345,3 +345,69 @@ test "std.meta.trait.isContainer" { |
| 345 | testing.expect(isContainer(TestEnum)); | 345 | testing.expect(isContainer(TestEnum)); |
| 346 | testing.expect(!isContainer(u8)); | 346 | testing.expect(!isContainer(u8)); |
| 347 | } | 347 | } |
| | 348 | |
| | 349 | pub 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 | |
| | 357 | test "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 | |
| | 372 | pub 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 | |
| | 380 | test "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 | |
| | 395 | pub 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 | |
| | 403 | test "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 | } |