iron_ingot/structs/vec4/
uvec4.rs

1use std::{
2  fmt::{self, Display, Formatter},
3  ops::{
4    Add, AddAssign, BitAnd, BitAndAssign, Div, DivAssign, Mul, MulAssign, Shl, ShlAssign, Shr,
5    ShrAssign, Sub, SubAssign,
6  },
7};
8
9use crate::{DVec4, FVec4, IVec4};
10
11#[derive(Copy, Clone, Debug)]
12pub struct Arg {
13  x: u32,
14  y: u32,
15  z: u32,
16  w: u32,
17}
18
19impl From<()> for Arg {
20  fn from(_: ()) -> Self {
21    Self {
22      x: 0,
23      y: 0,
24      z: 0,
25      w: 0,
26    }
27  }
28}
29
30impl From<u32> for Arg {
31  fn from(value: u32) -> Self {
32    Self {
33      x: value,
34      y: value,
35      z: value,
36      w: value,
37    }
38  }
39}
40
41impl From<(u32, u32, u32, u32)> for Arg {
42  fn from((x, y, z, w): (u32, u32, u32, u32)) -> Self {
43    Self { x, y, z, w }
44  }
45}
46
47#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
48pub struct UVec4 {
49  x: u32,
50  y: u32,
51  z: u32,
52  w: u32,
53}
54
55impl UVec4 {
56  pub fn new(arg: impl Into<Arg>) -> Self {
57    let Arg { x, y, z, w } = arg.into();
58    Self { x, y, z, w }
59  }
60
61  pub const fn get_x(&self) -> u32 {
62    self.x
63  }
64
65  pub fn set_x(&mut self, value: u32) {
66    self.x = value;
67  }
68
69  pub fn with_x(&self, value: u32) -> Self {
70    Self::new((value, self.y, self.z, self.w))
71  }
72
73  pub const fn get_y(&self) -> u32 {
74    self.y
75  }
76
77  pub fn set_y(&mut self, value: u32) {
78    self.y = value;
79  }
80
81  pub fn with_y(&self, value: u32) -> Self {
82    Self::new((self.x, value, self.z, self.w))
83  }
84
85  pub const fn get_z(&self) -> u32 {
86    self.z
87  }
88
89  pub fn set_z(&mut self, value: u32) {
90    self.z = value;
91  }
92
93  pub fn with_z(&self, value: u32) -> Self {
94    Self::new((self.x, self.y, value, self.w))
95  }
96
97  pub const fn get_w(&self) -> u32 {
98    self.w
99  }
100
101  pub fn set_w(&mut self, value: u32) {
102    self.w = value;
103  }
104
105  pub fn with_w(&self, value: u32) -> Self {
106    Self::new((self.x, self.y, self.z, value))
107  }
108
109  pub fn set(&mut self, other: Self) {
110    self.x = other.x;
111    self.y = other.y;
112    self.z = other.z;
113    self.w = other.w;
114  }
115}
116
117impl Add for UVec4 {
118  type Output = Self;
119
120  fn add(self, other: Self) -> Self::Output {
121    Self::new((
122      self.x + other.x,
123      self.y + other.y,
124      self.z + other.z,
125      self.w + other.w,
126    ))
127  }
128}
129
130impl AddAssign for UVec4 {
131  fn add_assign(&mut self, other: Self) {
132    self.x += other.x;
133    self.y += other.y;
134    self.z += other.z;
135    self.w += other.w;
136  }
137}
138
139impl Sub for UVec4 {
140  type Output = Self;
141
142  fn sub(self, other: Self) -> Self::Output {
143    Self::new((
144      self.x - other.x,
145      self.y - other.y,
146      self.z - other.z,
147      self.w - other.w,
148    ))
149  }
150}
151
152impl SubAssign for UVec4 {
153  fn sub_assign(&mut self, other: Self) {
154    self.x -= other.x;
155    self.y -= other.y;
156    self.z -= other.z;
157    self.w -= other.w;
158  }
159}
160
161impl Mul<u32> for UVec4 {
162  type Output = Self;
163
164  fn mul(self, value: u32) -> Self::Output {
165    Self::new((
166      self.x * value,
167      self.y * value,
168      self.z * value,
169      self.w * value,
170    ))
171  }
172}
173
174impl MulAssign<u32> for UVec4 {
175  fn mul_assign(&mut self, value: u32) {
176    self.x *= value;
177    self.y *= value;
178    self.z *= value;
179    self.w *= value;
180  }
181}
182
183impl Mul for UVec4 {
184  type Output = Self;
185
186  fn mul(self, other: Self) -> Self::Output {
187    Self::new((
188      self.x * other.x,
189      self.y * other.y,
190      self.z * other.z,
191      self.w * other.w,
192    ))
193  }
194}
195
196impl MulAssign for UVec4 {
197  fn mul_assign(&mut self, other: Self) {
198    self.x *= other.x;
199    self.y *= other.y;
200    self.z *= other.z;
201    self.w *= other.w;
202  }
203}
204
205impl Div<u32> for UVec4 {
206  type Output = Self;
207
208  fn div(self, value: u32) -> Self::Output {
209    debug_assert_ne!(
210      value, 0,
211      "value must not be equal to 0 to avoid causing division by zero error!"
212    );
213    Self::new((
214      self.x / value,
215      self.y / value,
216      self.z / value,
217      self.w / value,
218    ))
219  }
220}
221
222impl DivAssign<u32> for UVec4 {
223  fn div_assign(&mut self, value: u32) {
224    debug_assert_ne!(
225      value, 0,
226      "value must not be equal to 0 to avoid causing division by zero error!"
227    );
228    self.x /= value;
229    self.y /= value;
230    self.z /= value;
231    self.w /= value;
232  }
233}
234
235impl Div for UVec4 {
236  type Output = Self;
237
238  fn div(self, other: Self) -> Self::Output {
239    debug_assert_ne!(
240      other.x, 0,
241      "other.x must not be equal to 0 to avoid causing division by zero error!"
242    );
243    debug_assert_ne!(
244      other.y, 0,
245      "other.y must not be equal to 0 to avoid causing division by zero error!"
246    );
247    debug_assert_ne!(
248      other.z, 0,
249      "other.z must not be equal to 0 to avoid causing division by zero error!"
250    );
251    debug_assert_ne!(
252      other.w, 0,
253      "other.w must not be equal to 0 to avoid causing division by zero error!"
254    );
255    Self::new((
256      self.x / other.x,
257      self.y / other.y,
258      self.z / other.z,
259      self.w / other.w,
260    ))
261  }
262}
263
264impl DivAssign for UVec4 {
265  fn div_assign(&mut self, other: Self) {
266    debug_assert_ne!(
267      other.x, 0,
268      "other.x must not be equal to 0 to avoid causing division by zero error!"
269    );
270    debug_assert_ne!(
271      other.y, 0,
272      "other.y must not be equal to 0 to avoid causing division by zero error!"
273    );
274    debug_assert_ne!(
275      other.z, 0,
276      "other.z must not be equal to 0 to avoid causing division by zero error!"
277    );
278    debug_assert_ne!(
279      other.w, 0,
280      "other.w must not be equal to 0 to avoid causing division by zero error!"
281    );
282    self.x /= other.x;
283    self.y /= other.y;
284    self.z /= other.z;
285    self.w /= other.w;
286  }
287}
288
289impl BitAnd<u32> for UVec4 {
290  type Output = UVec4;
291
292  fn bitand(self, value: u32) -> Self::Output {
293    Self::new((
294      self.x & value,
295      self.y & value,
296      self.z & value,
297      self.w & value,
298    ))
299  }
300}
301
302impl BitAndAssign<u32> for UVec4 {
303  fn bitand_assign(&mut self, value: u32) {
304    self.x &= value;
305    self.y &= value;
306    self.z &= value;
307    self.w &= value;
308  }
309}
310
311impl BitAnd for UVec4 {
312  type Output = UVec4;
313
314  fn bitand(self, other: Self) -> Self::Output {
315    Self::new((
316      self.x & other.x,
317      self.y & other.y,
318      self.z & other.z,
319      self.w & other.w,
320    ))
321  }
322}
323
324impl BitAndAssign for UVec4 {
325  fn bitand_assign(&mut self, other: Self) {
326    self.x &= other.x;
327    self.y &= other.y;
328    self.z &= other.z;
329    self.w &= other.w;
330  }
331}
332
333impl Shl<u32> for UVec4 {
334  type Output = UVec4;
335
336  fn shl(self, value: u32) -> Self::Output {
337    Self::new((
338      self.x << value,
339      self.y << value,
340      self.z << value,
341      self.w << value,
342    ))
343  }
344}
345
346impl ShlAssign<u32> for UVec4 {
347  fn shl_assign(&mut self, value: u32) {
348    self.x <<= value;
349    self.y <<= value;
350    self.z <<= value;
351    self.w <<= value;
352  }
353}
354
355impl Shl for UVec4 {
356  type Output = UVec4;
357
358  fn shl(self, other: Self) -> Self::Output {
359    Self::new((
360      self.x << other.x,
361      self.y << other.y,
362      self.z << other.z,
363      self.w << other.w,
364    ))
365  }
366}
367
368impl ShlAssign for UVec4 {
369  fn shl_assign(&mut self, other: Self) {
370    self.x <<= other.x;
371    self.y <<= other.y;
372    self.z <<= other.z;
373    self.w <<= other.w;
374  }
375}
376
377impl Shr<u32> for UVec4 {
378  type Output = UVec4;
379
380  fn shr(self, value: u32) -> Self::Output {
381    Self::new((
382      self.x >> value,
383      self.y >> value,
384      self.z >> value,
385      self.w >> value,
386    ))
387  }
388}
389
390impl ShrAssign<u32> for UVec4 {
391  fn shr_assign(&mut self, value: u32) {
392    self.x >>= value;
393    self.y >>= value;
394    self.z >>= value;
395    self.w >>= value;
396  }
397}
398
399impl Shr for UVec4 {
400  type Output = UVec4;
401
402  fn shr(self, other: Self) -> Self::Output {
403    Self::new((
404      self.x >> other.x,
405      self.y >> other.y,
406      self.z >> other.z,
407      self.w >> other.w,
408    ))
409  }
410}
411
412impl ShrAssign for UVec4 {
413  fn shr_assign(&mut self, other: Self) {
414    self.x >>= other.x;
415    self.y >>= other.y;
416    self.z >>= other.z;
417    self.w >>= other.w;
418  }
419}
420
421impl Display for UVec4 {
422  fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), fmt::Error> {
423    write!(formatter, "{},{},{},{}", self.x, self.y, self.z, self.w)
424  }
425}
426
427impl From<DVec4> for UVec4 {
428  fn from(vec: DVec4) -> Self {
429    Self::new((
430      vec.get_x() as _,
431      vec.get_y() as _,
432      vec.get_z() as _,
433      vec.get_w() as _,
434    ))
435  }
436}
437
438impl From<FVec4> for UVec4 {
439  fn from(vec: FVec4) -> Self {
440    Self::new((
441      vec.get_x() as _,
442      vec.get_y() as _,
443      vec.get_z() as _,
444      vec.get_w() as _,
445    ))
446  }
447}
448
449impl From<IVec4> for UVec4 {
450  fn from(vec: IVec4) -> Self {
451    Self::new((
452      vec.get_x() as _,
453      vec.get_y() as _,
454      vec.get_z() as _,
455      vec.get_w() as _,
456    ))
457  }
458}