| ... | ... | @@ -614,3 +614,32 @@ pub fn LoggingWriter(comptime T: type, comptime scope: @Type(.EnumLiteral)) type |
| 614 | 614 | } |
| 615 | 615 | }; |
| 616 | 616 | } |
| 617 | |
| 618 | pub fn Partial(comptime T: type) type { |
| 619 | const fields_before = std.meta.fields(T); |
| 620 | var fields_after: [fields_before.len]std.builtin.Type.StructField = undefined; |
| 621 | inline for (fields_before, 0..) |item, i| { |
| 622 | fields_after[i] = std.builtin.Type.StructField{ |
| 623 | .name = item.name, |
| 624 | .type = ?item.type, |
| 625 | .default_value = &@as(?item.type, null), |
| 626 | .is_comptime = false, |
| 627 | .alignment = @alignOf(?item.type), |
| 628 | }; |
| 629 | } |
| 630 | return @Type(@unionInit(std.builtin.Type, "Struct", .{ |
| 631 | .layout = .Auto, |
| 632 | .backing_integer = null, |
| 633 | .fields = &fields_after, |
| 634 | .decls = &.{}, |
| 635 | .is_tuple = false, |
| 636 | })); |
| 637 | } |
| 638 | |
| 639 | pub fn coalescePartial(comptime T: type, into: T, from: Partial(T)) T { |
| 640 | var temp = into; |
| 641 | inline for (comptime std.meta.fieldNames(T)) |name| { |
| 642 | if (@field(from, name)) |val| @field(temp, name) = val; |
| 643 | } |
| 644 | return temp; |
| 645 | } |