| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | const Partial = extras.Partial; |
| 5 | |
| 6 | pub fn coalescePartial(comptime T: type, into: T, from: Partial(T)) T { |
| 7 | var temp = into; |
| 8 | inline for (comptime std.meta.fieldNames(T)) |name| { |
| 9 | if (@field(from, name)) |val| @field(temp, name) = val; |
| 10 | } |
| 11 | return temp; |
| 12 | } |
| 13 | |
| 14 | test { |
| 15 | const S = struct { |
| 16 | a: u8 = 4, |
| 17 | b: u8 = 7, |
| 18 | c: u8 = 1, |
| 19 | }; |
| 20 | try std.testing.expect(std.meta.eql(coalescePartial(S, .{}, .{ .b = 9 }), .{ |
| 21 | .a = 4, |
| 22 | .b = 9, |
| 23 | .c = 1, |
| 24 | })); |
| 25 | } |