Implement point operators with macro
[kaka/rust-sdl-test.git] / src / common.rs
CommitLineData
2836f506 1use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg};
296187ca 2
b0566120
TW
3pub type Nanoseconds = u64;
4
787dbfb4 5#[macro_export]
296187ca 6macro_rules! point {
6ba7aef1
TW
7 ( $x:expr, $y:expr ) => {
8 Point2D { x: $x, y: $y }
9 };
296187ca
TW
10}
11
b0566120 12#[derive(Debug, Default, Copy, Clone, PartialEq)]
296187ca
TW
13pub struct Point2D<T> {
14 pub x: T,
15 pub y: T,
16}
17
18impl Point2D<f64> {
19 pub fn length(self) -> f64 {
20 ((self.x * self.x) + (self.y * self.y)).sqrt()
21 }
22}
23
6cd86b94
TW
24macro_rules! point_op {
25 ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => {
26 impl<T: $trait<Output = T>> $trait<$Rhs> for Point2D<T> {
27 type Output = Self;
28
29 fn $fn(self, $rhs: $Rhs) -> Self {
30 Self {
31 x: self.x $op $x,
32 y: self.y $op $y,
33 }
34 }
2836f506 35 }
2836f506 36
6cd86b94
TW
37 impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point2D<T> {
38 fn $fn_assign(&mut self, $rhs: $Rhs) {
39 *self = Self {
40 x: self.x $op $x,
41 y: self.y $op $y,
42 }
43 }
2836f506
TW
44 }
45 }
46}
47
6cd86b94
TW
48point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D<T> => rhs.x, rhs.y);
49point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D<T> => rhs.x, rhs.y);
50point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D<T> => rhs.x, rhs.y);
51point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D<T> => rhs.x, rhs.y);
52point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1);
53point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1);
54point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1);
55point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1);
2836f506
TW
56
57////////// multiply point with scalar //////////////////////////////////////////
58impl<T: Mul<Output = T> + Copy> Mul<T> for Point2D<T> {
59 type Output = Self;
60
61 fn mul(self, rhs: T) -> Self {
62 Self {
63 x: self.x * rhs,
64 y: self.y * rhs,
65 }
66 }
67}
68
69impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point2D<T> {
70 fn mul_assign(&mut self, rhs: T) {
71 *self = Self {
72 x: self.x * rhs,
73 y: self.y * rhs,
74 }
75 }
76}
77
2836f506
TW
78////////// divide point with scalar ////////////////////////////////////////////
79impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> {
80 type Output = Self;
81
82 fn div(self, rhs: T) -> Self {
83 Self {
84 x: self.x / rhs,
85 y: self.y / rhs,
86 }
87 }
88}
89
90impl<T: Div<Output = T> + Copy> DivAssign<T> for Point2D<T> {
91 fn div_assign(&mut self, rhs: T) {
92 *self = Self {
93 x: self.x / rhs,
94 y: self.y / rhs,
95 }
96 }
97}
98
2836f506
TW
99impl<T: Neg<Output = T>> Neg for Point2D<T> {
100 type Output = Self;
101
102 fn neg(self) -> Self {
103 Self {
104 x: -self.x,
105 y: -self.y,
106 }
296187ca
TW
107 }
108}
109
b0566120
TW
110impl<T> From<(T, T)> for Point2D<T> {
111 fn from(item: (T, T)) -> Self {
112 Point2D {
113 x: item.0,
114 y: item.1,
115 }
116 }
117}
118
0b5024d1
TW
119#[macro_export]
120macro_rules! rect {
121 ( $x:expr, $y:expr ) => {
122 Rect { x: $x, y: $y }
123 };
124}
125
6edafdc0
TW
126#[derive(Default)]
127pub struct Rect<T> {
128 pub width: T,
129 pub height: T,
130}
131
6ba7aef1 132impl<T: Mul<Output = T> + Copy> Rect<T> {
6edafdc0
TW
133 #[allow(dead_code)]
134 pub fn area(&self) -> T {
6ba7aef1 135 self.width * self.height
6edafdc0
TW
136 }
137}
138
139impl<T> From<(T, T)> for Rect<T> {
140 fn from(item: (T, T)) -> Self {
6ba7aef1
TW
141 Rect {
142 width: item.0,
143 height: item.1,
144 }
6edafdc0
TW
145 }
146}
147
296187ca
TW
148#[cfg(test)]
149mod tests {
150 use super::*;
151
152 #[test]
153 fn immutable_copy_of_point() {
154 let a = point!(0, 0);
155 let mut b = a; // Copy
156 assert_eq!(a, b); // PartialEq
157 b.x = 1;
158 assert_ne!(a, b); // PartialEq
159 }
160
161 #[test]
162 fn add_points() {
163 let mut a = point!(1, 0);
164 assert_eq!(a + point!(2, 2), point!(3, 2)); // Add
165 a += point!(2, 2); // AddAssign
166 assert_eq!(a, point!(3, 2));
2836f506
TW
167 assert_eq!(point!(1, 0) + (2, 3), point!(3, 3));
168 }
169
170 #[test]
171 fn sub_points() {
172 let mut a = point!(1, 0);
173 assert_eq!(a - point!(2, 2), point!(-1, -2));
174 a -= point!(2, 2);
175 assert_eq!(a, point!(-1, -2));
6cd86b94 176 assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3));
2836f506
TW
177 }
178
179 #[test]
180 fn mul_points() {
181 let mut a = point!(1, 2);
182 assert_eq!(a * 2, point!(2, 4));
183 assert_eq!(a * point!(2, 3), point!(2, 6));
184 a *= 2;
185 assert_eq!(a, point!(2, 4));
186 a *= point!(3, 1);
187 assert_eq!(a, point!(6, 4));
6cd86b94 188 assert_eq!(point!(1, 0) * (2, 3), point!(2, 0));
2836f506
TW
189 }
190
191 #[test]
192 fn div_points() {
193 let mut a = point!(4, 8);
194 assert_eq!(a / 2, point!(2, 4));
195 assert_eq!(a / point!(2, 4), point!(2, 2));
196 a /= 2;
197 assert_eq!(a, point!(2, 4));
198 a /= point!(2, 4);
199 assert_eq!(a, point!(1, 1));
6cd86b94 200 assert_eq!(point!(6, 3) / (2, 3), point!(3, 1));
2836f506
TW
201 }
202
203 #[test]
204 fn neg_point() {
205 assert_eq!(point!(1, 1), -point!(-1, -1));
296187ca 206 }
6edafdc0
TW
207
208 #[test]
209 fn area_for_rect_of_multipliable_type() {
210 let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
6ba7aef1 211 assert_eq!(r.area(), 30 * 20);
6edafdc0
TW
212 // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String
213 }
296187ca 214}