Commit | Line | Data |
---|---|---|
6edafdc0 | 1 | use std::ops::{Add, AddAssign, Mul}; |
296187ca | 2 | |
b0566120 TW |
3 | pub type Nanoseconds = u64; |
4 | ||
787dbfb4 | 5 | #[macro_export] |
296187ca | 6 | macro_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 |
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 | ||
6ba7aef1 | 24 | impl<T: Add<Output = T>> Add for Point2D<T> { |
296187ca TW |
25 | type Output = Point2D<T>; |
26 | ||
27 | fn add(self, rhs: Point2D<T>) -> Self::Output { | |
6ba7aef1 TW |
28 | Point2D { |
29 | x: self.x + rhs.x, | |
30 | y: self.y + rhs.y, | |
31 | } | |
296187ca TW |
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 | ||
b0566120 TW |
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 | ||
0b5024d1 TW |
51 | #[macro_export] |
52 | macro_rules! rect { | |
53 | ( $x:expr, $y:expr ) => { | |
54 | Rect { x: $x, y: $y } | |
55 | }; | |
56 | } | |
57 | ||
6edafdc0 TW |
58 | #[derive(Default)] |
59 | pub struct Rect<T> { | |
60 | pub width: T, | |
61 | pub height: T, | |
62 | } | |
63 | ||
6ba7aef1 | 64 | impl<T: Mul<Output = T> + Copy> Rect<T> { |
6edafdc0 TW |
65 | #[allow(dead_code)] |
66 | pub fn area(&self) -> T { | |
6ba7aef1 | 67 | self.width * self.height |
6edafdc0 TW |
68 | } |
69 | } | |
70 | ||
71 | impl<T> From<(T, T)> for Rect<T> { | |
72 | fn from(item: (T, T)) -> Self { | |
6ba7aef1 TW |
73 | Rect { |
74 | width: item.0, | |
75 | height: item.1, | |
76 | } | |
6edafdc0 TW |
77 | } |
78 | } | |
79 | ||
296187ca TW |
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 | } | |
6edafdc0 TW |
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 | |
6ba7aef1 | 104 | assert_eq!(r.area(), 30 * 20); |
6edafdc0 TW |
105 | // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String |
106 | } | |
296187ca | 107 | } |