From f936714142d30ba71c0389f62c89fcf7efc8f52c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Tue, 23 Jul 2024 15:02:26 +0900 Subject: [PATCH] geometry: type ergonomics in rectangle --- src/geometry.zig | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/geometry.zig b/src/geometry.zig index 25ba638..1f126f4 100644 --- a/src/geometry.zig +++ b/src/geometry.zig @@ -9,6 +9,10 @@ const expectEqualDeep = std.testing.expectEqualDeep; /// A generic rectangle object with some convenience functionality. pub fn Rectangle(comptime T: type) type { + switch (@typeInfo(T)) { + .Int, .Float => {}, + else => @compileError("Unsupported type " ++ @typeName(T) ++ " for Rectangle"), + } return struct { const Self = @This(); l: T, @@ -64,25 +68,25 @@ pub fn Rectangle(comptime T: type) type { } /// Returns the width of the rectangle. - pub fn width(self: Self) T { + pub fn width(self: Self) if (@typeInfo(T) == .Int) usize else T { return if (self.isEmpty()) 0 else switch (@typeInfo(T)) { - .Int => self.r - self.l + 1, + .Int => @intCast(self.r - self.l + 1), .Float => self.r - self.t, else => @compileError("Unsupported type " ++ @typeName(T) ++ " for Rectangle"), }; } /// Returns the height of the rectangle. - pub fn height(self: Self) T { + pub fn height(self: Self) if (@typeInfo(T) == .Int) usize else T { return if (self.isEmpty()) 0 else switch (@typeInfo(T)) { - .Int => self.b - self.t + 1, + .Int => @intCast(self.b - self.t + 1), .Float => self.b - self.t, else => @compileError("Unsupported type " ++ @typeName(T) ++ " for Rectangle"), }; } /// Returns the area of the rectangle - pub fn area(self: Self) T { + pub fn area(self: Self) if (@typeInfo(T) == .Int) usize else T { return self.height() * self.width(); }