wcanvas/geo/
dims.rs

1use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
2
3/// Represents the dimensions of an object.
4#[derive(Debug, Clone, Copy, Eq, PartialEq)]
5pub struct Dims {
6    pub width: u32,
7    pub height: u32,
8}
9
10impl Dims {
11    /// Construct a pair of dimensions.
12    pub fn new(width: u32, height: u32) -> Self {
13        Self { width, height }
14    }
15}
16
17impl Add<Dims> for Dims {
18    type Output = Dims;
19
20    fn add(self, rhs: Dims) -> Self::Output {
21        Self::new(self.width + rhs.width, self.height + rhs.height)
22    }
23}
24
25impl AddAssign<Dims> for Dims {
26    fn add_assign(&mut self, rhs: Dims) {
27        self.width += rhs.width;
28        self.height += rhs.height;
29    }
30}
31
32impl Sub<Dims> for Dims {
33    type Output = Dims;
34
35    fn sub(self, rhs: Dims) -> Self::Output {
36        Self::new(self.width - rhs.width, self.height - rhs.height)
37    }
38}
39
40impl SubAssign<Dims> for Dims {
41    fn sub_assign(&mut self, rhs: Dims) {
42        self.width -= rhs.width;
43        self.height -= rhs.height;
44    }
45}
46
47impl Mul<u32> for Dims {
48    type Output = Self;
49
50    fn mul(self, rhs: u32) -> Self::Output {
51        Self::new(self.width * rhs, self.height * rhs)
52    }
53}
54
55impl MulAssign<u32> for Dims {
56    fn mul_assign(&mut self, rhs: u32) {
57        self.width *= rhs;
58        self.height *= rhs;
59    }
60}
61
62impl Mul<Dims> for Dims {
63    type Output = Self;
64
65    fn mul(self, rhs: Dims) -> Self::Output {
66        Self::new(self.width * rhs.width, self.height * rhs.height)
67    }
68}
69
70impl MulAssign<Dims> for Dims {
71    fn mul_assign(&mut self, rhs: Dims) {
72        self.width *= rhs.width;
73        self.height *= rhs.height;
74    }
75}
76
77impl Div<u32> for Dims {
78    type Output = Self;
79
80    fn div(self, rhs: u32) -> Self::Output {
81        Self::new(self.width / rhs, self.height / rhs)
82    }
83}
84
85impl DivAssign<u32> for Dims {
86    fn div_assign(&mut self, rhs: u32) {
87        self.width /= rhs;
88        self.height /= rhs;
89    }
90}
91
92impl Div<Dims> for Dims {
93    type Output = Self;
94
95    fn div(self, rhs: Dims) -> Self::Output {
96        Self::new(self.width / rhs.width, self.height / rhs.height)
97    }
98}
99
100impl DivAssign<Dims> for Dims {
101    fn div_assign(&mut self, rhs: Dims) {
102        self.width /= rhs.width;
103        self.height /= rhs.height;
104    }
105}