vector math experiments

This commit is contained in:
David Allemang
2024-03-27 17:40:39 -04:00
parent 2f70c43c00
commit d4196644df
2 changed files with 126 additions and 0 deletions

29
box.zig Normal file
View File

@@ -0,0 +1,29 @@
const std = @import("std");
pub fn Box(comptime T: type) type {
return struct {
val: T,
};
}
pub fn AddBoxType(comptime LHS: type, comptime RHS: type) type {
const x = std.mem.zeroes(std.meta.FieldType(LHS, .val));
const y = std.mem.zeroes(std.meta.FieldType(RHS, .val));
return Box(@TypeOf(x + y));
}
pub fn addbox(lhs: anytype, rhs: anytype) AddBoxType(@TypeOf(lhs), @TypeOf(rhs)) {
return .{ .val = lhs.val + rhs.val };
}
test {
std.testing.refAllDecls(@This());
}
test "widen" {
const foo: Box(u8) = .{ .val = 99 };
const bar: Box(u16) = .{ .val = 599 };
const actual = addbox(foo, bar);
const expected: Box(u16) = .{ .val = 698 };
try std.testing.expectEqual(expected, actual);
}