52b732482f6ee4ea6b8013786de9783feefcb9f9
[kaka/rust-sdl-test.git] / src / common.rs
1 use std::ops::{Add, AddAssign, Mul};
2
3 pub type Nanoseconds = u64;
4
5 #[macro_export]
6 macro_rules! point {
7     ( $x:expr, $y:expr ) => {
8         Point2D { x: $x, y: $y }
9     };
10 }
11
12 #[derive(Debug, Default, Copy, Clone, PartialEq)]
13 pub struct Point2D<T> {
14     pub x: T,
15     pub y: T,
16 }
17
18 impl Point2D<f64> {
19     pub fn length(self) -> f64 {
20         ((self.x * self.x) + (self.y * self.y)).sqrt()
21     }
22 }
23
24 impl<T: Add<Output = T>> Add for Point2D<T> {
25     type Output = Point2D<T>;
26
27     fn add(self, rhs: Point2D<T>) -> Self::Output {
28         Point2D {
29             x: self.x + rhs.x,
30             y: self.y + rhs.y,
31         }
32     }
33 }
34
35 impl<T: AddAssign> AddAssign for Point2D<T> {
36     fn add_assign(&mut self, rhs: Point2D<T>) {
37         self.x += rhs.x;
38         self.y += rhs.y;
39     }
40 }
41
42 impl<T> From<(T, T)> for Point2D<T> {
43     fn from(item: (T, T)) -> Self {
44         Point2D {
45             x: item.0,
46             y: item.1,
47         }
48     }
49 }
50
51 #[macro_export]
52 macro_rules! rect {
53     ( $x:expr, $y:expr ) => {
54         Rect { x: $x, y: $y }
55     };
56 }
57
58 #[derive(Default)]
59 pub struct Rect<T> {
60     pub width: T,
61     pub height: T,
62 }
63
64 impl<T: Mul<Output = T> + Copy> Rect<T> {
65     #[allow(dead_code)]
66     pub fn area(&self) -> T {
67         self.width * self.height
68     }
69 }
70
71 impl<T> From<(T, T)> for Rect<T> {
72     fn from(item: (T, T)) -> Self {
73         Rect {
74             width: item.0,
75             height: item.1,
76         }
77     }
78 }
79
80 #[cfg(test)]
81 mod tests {
82     use super::*;
83
84     #[test]
85     fn immutable_copy_of_point() {
86         let a = point!(0, 0);
87         let mut b = a; // Copy
88         assert_eq!(a, b); // PartialEq
89         b.x = 1;
90         assert_ne!(a, b); // PartialEq
91     }
92
93     #[test]
94     fn add_points() {
95         let mut a = point!(1, 0);
96         assert_eq!(a + point!(2, 2), point!(3, 2)); // Add
97         a += point!(2, 2); // AddAssign
98         assert_eq!(a, point!(3, 2));
99     }
100
101     #[test]
102     fn area_for_rect_of_multipliable_type() {
103         let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
104         assert_eq!(r.area(), 30 * 20);
105         // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String
106     }
107 }