1#[derive(Debug, Clone, Copy)]
2pub struct Size {
3 width: i32,
4 height: i32,
5}
6
7impl Size {
8 pub fn new<T: ToPrimitive>(width: T, height: T) -> Self {
9 Self {
10 width: width.to_i32().unwrap_or(0),
11 height: height.to_i32().unwrap_or(0),
12 }
13 }
14
15 pub const ZERO: Self = Self {
16 width: 0,
17 height: 0,
18 };
19 pub const ONE: Self = Self {
20 width: 1,
21 height: 1,
22 };
23}
24
25impl Default for Size {
26 fn default() -> Self {
27 Self::ZERO
28 }
29}
30
31impl Into<wgpu::Extent3d> for Size {
32 fn into(self) -> wgpu::Extent3d {
33 wgpu::Extent3d {
34 width: self.width as u32,
35 height: self.height as u32,
36 depth_or_array_layers: 1,
37 }
38 }
39}
40
41use std::ops::*;
42
43use num_traits::ToPrimitive;
44use winit::dpi::PhysicalSize;
45
46impl Add for Size {
47 type Output = Self;
48
49 fn add(self, rhs: Self) -> Self {
50 Self {
51 width: self.width + rhs.width,
52 height: self.height + rhs.height,
53 }
54 }
55}
56
57impl AddAssign for Size {
58 fn add_assign(&mut self, rhs: Self) {
59 self.width += rhs.width;
60 self.height += rhs.height;
61 }
62}
63
64impl Sub for Size {
65 type Output = Self;
66
67 fn sub(self, rhs: Self) -> Self {
68 Self {
69 width: self.width - rhs.width,
70 height: self.height - rhs.height,
71 }
72 }
73}
74
75impl SubAssign for Size {
76 fn sub_assign(&mut self, rhs: Self) {
77 self.width -= rhs.width;
78 self.height -= rhs.height;
79 }
80}
81
82impl Mul<i32> for Size {
83 type Output = Self;
84
85 fn mul(self, rhs: i32) -> Self {
86 Self {
87 width: self.width * rhs,
88 height: self.height * rhs,
89 }
90 }
91}
92
93impl MulAssign<i32> for Size {
94 fn mul_assign(&mut self, rhs: i32) {
95 self.width *= rhs;
96 self.height *= rhs;
97 }
98}
99
100impl Div<i32> for Size {
101 type Output = Self;
102
103 fn div(self, rhs: i32) -> Self {
104 Self {
105 width: self.width / rhs,
106 height: self.height / rhs,
107 }
108 }
109}
110
111impl DivAssign<i32> for Size {
112 fn div_assign(&mut self, rhs: i32) {
113 self.width /= rhs;
114 self.height /= rhs;
115 }
116}
117
118impl Neg for Size {
119 type Output = Self;
120
121 fn neg(self) -> Self {
122 Self {
123 width: -self.width,
124 height: -self.height,
125 }
126 }
127}
128
129impl PartialEq for Size {
130 fn eq(&self, other: &Self) -> bool {
131 self.width == other.width && self.height == other.height
132 }
133}
134
135impl Eq for Size {}
136
137impl Into<PhysicalSize<u32>> for Size {
138 fn into(self) -> PhysicalSize<u32> {
139 PhysicalSize::new(self.width as u32, self.height as u32)
140 }
141}
142
143impl Into<PhysicalSize<i32>> for Size {
144 fn into(self) -> PhysicalSize<i32> {
145 PhysicalSize::new(self.width, self.height)
146 }
147}
148
149impl From<PhysicalSize<i32>> for Size {
150 fn from(pos: PhysicalSize<i32>) -> Self {
151 Self {
152 width: pos.width,
153 height: pos.height,
154 }
155 }
156}
157
158impl From<PhysicalSize<u32>> for Size {
159 fn from(pos: PhysicalSize<u32>) -> Self {
160 Self {
161 width: pos.width as i32,
162 height: pos.height as i32,
163 }
164 }
165}