@@ -9,6 +9,10 @@ const expectEqualDeep = std.testing.expectEqualDeep;
9
9
10
10
/// A generic rectangle object with some convenience functionality.
11
11
pub fn Rectangle (comptime T : type ) type {
12
+ switch (@typeInfo (T )) {
13
+ .Int , .Float = > {},
14
+ else = > @compileError ("Unsupported type " ++ @typeName (T ) ++ " for Rectangle" ),
15
+ }
12
16
return struct {
13
17
const Self = @This ();
14
18
l : T ,
@@ -64,25 +68,25 @@ pub fn Rectangle(comptime T: type) type {
64
68
}
65
69
66
70
/// Returns the width of the rectangle.
67
- pub fn width (self : Self ) T {
71
+ pub fn width (self : Self ) if ( @typeInfo ( T ) == .Int ) usize else T {
68
72
return if (self .isEmpty ()) 0 else switch (@typeInfo (T )) {
69
- .Int = > self .r - self .l + 1 ,
73
+ .Int = > @intCast ( self .r - self .l + 1 ) ,
70
74
.Float = > self .r - self .t ,
71
75
else = > @compileError ("Unsupported type " ++ @typeName (T ) ++ " for Rectangle" ),
72
76
};
73
77
}
74
78
75
79
/// Returns the height of the rectangle.
76
- pub fn height (self : Self ) T {
80
+ pub fn height (self : Self ) if ( @typeInfo ( T ) == .Int ) usize else T {
77
81
return if (self .isEmpty ()) 0 else switch (@typeInfo (T )) {
78
- .Int = > self .b - self .t + 1 ,
82
+ .Int = > @intCast ( self .b - self .t + 1 ) ,
79
83
.Float = > self .b - self .t ,
80
84
else = > @compileError ("Unsupported type " ++ @typeName (T ) ++ " for Rectangle" ),
81
85
};
82
86
}
83
87
84
88
/// Returns the area of the rectangle
85
- pub fn area (self : Self ) T {
89
+ pub fn area (self : Self ) if ( @typeInfo ( T ) == .Int ) usize else T {
86
90
return self .height () * self .width ();
87
91
}
88
92
0 commit comments