1use crate::{BVec2, I16Vec2, I64Vec2, IVec3, U16Vec2, U64Vec2, UVec2};
4
5#[cfg(not(target_arch = "spirv"))]
6use core::fmt;
7use core::iter::{Product, Sum};
8use core::{f32, ops::*};
9
10#[inline(always)]
12#[must_use]
13pub const fn ivec2(x: i32, y: i32) -> IVec2 {
14 IVec2::new(x, y)
15}
16
17#[cfg_attr(not(target_arch = "spirv"), derive(Hash))]
19#[derive(Clone, Copy, PartialEq, Eq)]
20#[cfg_attr(feature = "cuda", repr(align(8)))]
21#[cfg_attr(not(target_arch = "spirv"), repr(C))]
22#[cfg_attr(target_arch = "spirv", repr(simd))]
23pub struct IVec2 {
24 pub x: i32,
25 pub y: i32,
26}
27
28impl IVec2 {
29 pub const ZERO: Self = Self::splat(0);
31
32 pub const ONE: Self = Self::splat(1);
34
35 pub const NEG_ONE: Self = Self::splat(-1);
37
38 pub const MIN: Self = Self::splat(i32::MIN);
40
41 pub const MAX: Self = Self::splat(i32::MAX);
43
44 pub const X: Self = Self::new(1, 0);
46
47 pub const Y: Self = Self::new(0, 1);
49
50 pub const NEG_X: Self = Self::new(-1, 0);
52
53 pub const NEG_Y: Self = Self::new(0, -1);
55
56 pub const AXES: [Self; 2] = [Self::X, Self::Y];
58
59 #[inline(always)]
61 #[must_use]
62 pub const fn new(x: i32, y: i32) -> Self {
63 Self { x, y }
64 }
65
66 #[inline]
68 #[must_use]
69 pub const fn splat(v: i32) -> Self {
70 Self { x: v, y: v }
71 }
72
73 #[inline]
79 #[must_use]
80 pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
81 Self {
82 x: if mask.test(0) { if_true.x } else { if_false.x },
83 y: if mask.test(1) { if_true.y } else { if_false.y },
84 }
85 }
86
87 #[inline]
89 #[must_use]
90 pub const fn from_array(a: [i32; 2]) -> Self {
91 Self::new(a[0], a[1])
92 }
93
94 #[inline]
96 #[must_use]
97 pub const fn to_array(&self) -> [i32; 2] {
98 [self.x, self.y]
99 }
100
101 #[inline]
107 #[must_use]
108 pub const fn from_slice(slice: &[i32]) -> Self {
109 Self::new(slice[0], slice[1])
110 }
111
112 #[inline]
118 pub fn write_to_slice(self, slice: &mut [i32]) {
119 slice[0] = self.x;
120 slice[1] = self.y;
121 }
122
123 #[inline]
125 #[must_use]
126 pub const fn extend(self, z: i32) -> IVec3 {
127 IVec3::new(self.x, self.y, z)
128 }
129
130 #[inline]
132 #[must_use]
133 pub fn dot(self, rhs: Self) -> i32 {
134 (self.x * rhs.x) + (self.y * rhs.y)
135 }
136
137 #[inline]
139 #[must_use]
140 pub fn dot_into_vec(self, rhs: Self) -> Self {
141 Self::splat(self.dot(rhs))
142 }
143
144 #[inline]
148 #[must_use]
149 pub fn min(self, rhs: Self) -> Self {
150 Self {
151 x: self.x.min(rhs.x),
152 y: self.y.min(rhs.y),
153 }
154 }
155
156 #[inline]
160 #[must_use]
161 pub fn max(self, rhs: Self) -> Self {
162 Self {
163 x: self.x.max(rhs.x),
164 y: self.y.max(rhs.y),
165 }
166 }
167
168 #[inline]
176 #[must_use]
177 pub fn clamp(self, min: Self, max: Self) -> Self {
178 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
179 self.max(min).min(max)
180 }
181
182 #[inline]
186 #[must_use]
187 pub fn min_element(self) -> i32 {
188 self.x.min(self.y)
189 }
190
191 #[inline]
195 #[must_use]
196 pub fn max_element(self) -> i32 {
197 self.x.max(self.y)
198 }
199
200 #[inline]
206 #[must_use]
207 pub fn cmpeq(self, rhs: Self) -> BVec2 {
208 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
209 }
210
211 #[inline]
217 #[must_use]
218 pub fn cmpne(self, rhs: Self) -> BVec2 {
219 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
220 }
221
222 #[inline]
228 #[must_use]
229 pub fn cmpge(self, rhs: Self) -> BVec2 {
230 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
231 }
232
233 #[inline]
239 #[must_use]
240 pub fn cmpgt(self, rhs: Self) -> BVec2 {
241 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
242 }
243
244 #[inline]
250 #[must_use]
251 pub fn cmple(self, rhs: Self) -> BVec2 {
252 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
253 }
254
255 #[inline]
261 #[must_use]
262 pub fn cmplt(self, rhs: Self) -> BVec2 {
263 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
264 }
265
266 #[inline]
268 #[must_use]
269 pub fn abs(self) -> Self {
270 Self {
271 x: self.x.abs(),
272 y: self.y.abs(),
273 }
274 }
275
276 #[inline]
282 #[must_use]
283 pub fn signum(self) -> Self {
284 Self {
285 x: self.x.signum(),
286 y: self.y.signum(),
287 }
288 }
289
290 #[inline]
295 #[must_use]
296 pub fn is_negative_bitmask(self) -> u32 {
297 (self.x.is_negative() as u32) | (self.y.is_negative() as u32) << 1
298 }
299
300 #[doc(alias = "magnitude2")]
302 #[inline]
303 #[must_use]
304 pub fn length_squared(self) -> i32 {
305 self.dot(self)
306 }
307
308 #[inline]
310 #[must_use]
311 pub fn distance_squared(self, rhs: Self) -> i32 {
312 (self - rhs).length_squared()
313 }
314
315 #[inline]
320 #[must_use]
321 pub fn div_euclid(self, rhs: Self) -> Self {
322 Self::new(self.x.div_euclid(rhs.x), self.y.div_euclid(rhs.y))
323 }
324
325 #[inline]
332 #[must_use]
333 pub fn rem_euclid(self, rhs: Self) -> Self {
334 Self::new(self.x.rem_euclid(rhs.x), self.y.rem_euclid(rhs.y))
335 }
336
337 #[inline]
339 #[must_use]
340 pub fn perp(self) -> Self {
341 Self {
342 x: -self.y,
343 y: self.x,
344 }
345 }
346
347 #[doc(alias = "wedge")]
350 #[doc(alias = "cross")]
351 #[doc(alias = "determinant")]
352 #[inline]
353 #[must_use]
354 pub fn perp_dot(self, rhs: Self) -> i32 {
355 (self.x * rhs.y) - (self.y * rhs.x)
356 }
357
358 #[inline]
362 #[must_use]
363 pub fn rotate(self, rhs: Self) -> Self {
364 Self {
365 x: self.x * rhs.x - self.y * rhs.y,
366 y: self.y * rhs.x + self.x * rhs.y,
367 }
368 }
369
370 #[inline]
372 #[must_use]
373 pub fn as_vec2(&self) -> crate::Vec2 {
374 crate::Vec2::new(self.x as f32, self.y as f32)
375 }
376
377 #[inline]
379 #[must_use]
380 pub fn as_dvec2(&self) -> crate::DVec2 {
381 crate::DVec2::new(self.x as f64, self.y as f64)
382 }
383
384 #[inline]
386 #[must_use]
387 pub fn as_i16vec2(&self) -> crate::I16Vec2 {
388 crate::I16Vec2::new(self.x as i16, self.y as i16)
389 }
390
391 #[inline]
393 #[must_use]
394 pub fn as_u16vec2(&self) -> crate::U16Vec2 {
395 crate::U16Vec2::new(self.x as u16, self.y as u16)
396 }
397
398 #[inline]
400 #[must_use]
401 pub fn as_uvec2(&self) -> crate::UVec2 {
402 crate::UVec2::new(self.x as u32, self.y as u32)
403 }
404
405 #[inline]
407 #[must_use]
408 pub fn as_i64vec2(&self) -> crate::I64Vec2 {
409 crate::I64Vec2::new(self.x as i64, self.y as i64)
410 }
411
412 #[inline]
414 #[must_use]
415 pub fn as_u64vec2(&self) -> crate::U64Vec2 {
416 crate::U64Vec2::new(self.x as u64, self.y as u64)
417 }
418
419 #[inline]
423 #[must_use]
424 pub const fn wrapping_add(self, rhs: Self) -> Self {
425 Self {
426 x: self.x.wrapping_add(rhs.x),
427 y: self.y.wrapping_add(rhs.y),
428 }
429 }
430
431 #[inline]
435 #[must_use]
436 pub const fn wrapping_sub(self, rhs: Self) -> Self {
437 Self {
438 x: self.x.wrapping_sub(rhs.x),
439 y: self.y.wrapping_sub(rhs.y),
440 }
441 }
442
443 #[inline]
447 #[must_use]
448 pub const fn wrapping_mul(self, rhs: Self) -> Self {
449 Self {
450 x: self.x.wrapping_mul(rhs.x),
451 y: self.y.wrapping_mul(rhs.y),
452 }
453 }
454
455 #[inline]
459 #[must_use]
460 pub const fn wrapping_div(self, rhs: Self) -> Self {
461 Self {
462 x: self.x.wrapping_div(rhs.x),
463 y: self.y.wrapping_div(rhs.y),
464 }
465 }
466
467 #[inline]
471 #[must_use]
472 pub const fn saturating_add(self, rhs: Self) -> Self {
473 Self {
474 x: self.x.saturating_add(rhs.x),
475 y: self.y.saturating_add(rhs.y),
476 }
477 }
478
479 #[inline]
483 #[must_use]
484 pub const fn saturating_sub(self, rhs: Self) -> Self {
485 Self {
486 x: self.x.saturating_sub(rhs.x),
487 y: self.y.saturating_sub(rhs.y),
488 }
489 }
490
491 #[inline]
495 #[must_use]
496 pub const fn saturating_mul(self, rhs: Self) -> Self {
497 Self {
498 x: self.x.saturating_mul(rhs.x),
499 y: self.y.saturating_mul(rhs.y),
500 }
501 }
502
503 #[inline]
507 #[must_use]
508 pub const fn saturating_div(self, rhs: Self) -> Self {
509 Self {
510 x: self.x.saturating_div(rhs.x),
511 y: self.y.saturating_div(rhs.y),
512 }
513 }
514}
515
516impl Default for IVec2 {
517 #[inline(always)]
518 fn default() -> Self {
519 Self::ZERO
520 }
521}
522
523impl Div<IVec2> for IVec2 {
524 type Output = Self;
525 #[inline]
526 fn div(self, rhs: Self) -> Self {
527 Self {
528 x: self.x.div(rhs.x),
529 y: self.y.div(rhs.y),
530 }
531 }
532}
533
534impl DivAssign<IVec2> for IVec2 {
535 #[inline]
536 fn div_assign(&mut self, rhs: Self) {
537 self.x.div_assign(rhs.x);
538 self.y.div_assign(rhs.y);
539 }
540}
541
542impl Div<i32> for IVec2 {
543 type Output = Self;
544 #[inline]
545 fn div(self, rhs: i32) -> Self {
546 Self {
547 x: self.x.div(rhs),
548 y: self.y.div(rhs),
549 }
550 }
551}
552
553impl DivAssign<i32> for IVec2 {
554 #[inline]
555 fn div_assign(&mut self, rhs: i32) {
556 self.x.div_assign(rhs);
557 self.y.div_assign(rhs);
558 }
559}
560
561impl Div<IVec2> for i32 {
562 type Output = IVec2;
563 #[inline]
564 fn div(self, rhs: IVec2) -> IVec2 {
565 IVec2 {
566 x: self.div(rhs.x),
567 y: self.div(rhs.y),
568 }
569 }
570}
571
572impl Mul<IVec2> for IVec2 {
573 type Output = Self;
574 #[inline]
575 fn mul(self, rhs: Self) -> Self {
576 Self {
577 x: self.x.mul(rhs.x),
578 y: self.y.mul(rhs.y),
579 }
580 }
581}
582
583impl MulAssign<IVec2> for IVec2 {
584 #[inline]
585 fn mul_assign(&mut self, rhs: Self) {
586 self.x.mul_assign(rhs.x);
587 self.y.mul_assign(rhs.y);
588 }
589}
590
591impl Mul<i32> for IVec2 {
592 type Output = Self;
593 #[inline]
594 fn mul(self, rhs: i32) -> Self {
595 Self {
596 x: self.x.mul(rhs),
597 y: self.y.mul(rhs),
598 }
599 }
600}
601
602impl MulAssign<i32> for IVec2 {
603 #[inline]
604 fn mul_assign(&mut self, rhs: i32) {
605 self.x.mul_assign(rhs);
606 self.y.mul_assign(rhs);
607 }
608}
609
610impl Mul<IVec2> for i32 {
611 type Output = IVec2;
612 #[inline]
613 fn mul(self, rhs: IVec2) -> IVec2 {
614 IVec2 {
615 x: self.mul(rhs.x),
616 y: self.mul(rhs.y),
617 }
618 }
619}
620
621impl Add<IVec2> for IVec2 {
622 type Output = Self;
623 #[inline]
624 fn add(self, rhs: Self) -> Self {
625 Self {
626 x: self.x.add(rhs.x),
627 y: self.y.add(rhs.y),
628 }
629 }
630}
631
632impl AddAssign<IVec2> for IVec2 {
633 #[inline]
634 fn add_assign(&mut self, rhs: Self) {
635 self.x.add_assign(rhs.x);
636 self.y.add_assign(rhs.y);
637 }
638}
639
640impl Add<i32> for IVec2 {
641 type Output = Self;
642 #[inline]
643 fn add(self, rhs: i32) -> Self {
644 Self {
645 x: self.x.add(rhs),
646 y: self.y.add(rhs),
647 }
648 }
649}
650
651impl AddAssign<i32> for IVec2 {
652 #[inline]
653 fn add_assign(&mut self, rhs: i32) {
654 self.x.add_assign(rhs);
655 self.y.add_assign(rhs);
656 }
657}
658
659impl Add<IVec2> for i32 {
660 type Output = IVec2;
661 #[inline]
662 fn add(self, rhs: IVec2) -> IVec2 {
663 IVec2 {
664 x: self.add(rhs.x),
665 y: self.add(rhs.y),
666 }
667 }
668}
669
670impl Sub<IVec2> for IVec2 {
671 type Output = Self;
672 #[inline]
673 fn sub(self, rhs: Self) -> Self {
674 Self {
675 x: self.x.sub(rhs.x),
676 y: self.y.sub(rhs.y),
677 }
678 }
679}
680
681impl SubAssign<IVec2> for IVec2 {
682 #[inline]
683 fn sub_assign(&mut self, rhs: IVec2) {
684 self.x.sub_assign(rhs.x);
685 self.y.sub_assign(rhs.y);
686 }
687}
688
689impl Sub<i32> for IVec2 {
690 type Output = Self;
691 #[inline]
692 fn sub(self, rhs: i32) -> Self {
693 Self {
694 x: self.x.sub(rhs),
695 y: self.y.sub(rhs),
696 }
697 }
698}
699
700impl SubAssign<i32> for IVec2 {
701 #[inline]
702 fn sub_assign(&mut self, rhs: i32) {
703 self.x.sub_assign(rhs);
704 self.y.sub_assign(rhs);
705 }
706}
707
708impl Sub<IVec2> for i32 {
709 type Output = IVec2;
710 #[inline]
711 fn sub(self, rhs: IVec2) -> IVec2 {
712 IVec2 {
713 x: self.sub(rhs.x),
714 y: self.sub(rhs.y),
715 }
716 }
717}
718
719impl Rem<IVec2> for IVec2 {
720 type Output = Self;
721 #[inline]
722 fn rem(self, rhs: Self) -> Self {
723 Self {
724 x: self.x.rem(rhs.x),
725 y: self.y.rem(rhs.y),
726 }
727 }
728}
729
730impl RemAssign<IVec2> for IVec2 {
731 #[inline]
732 fn rem_assign(&mut self, rhs: Self) {
733 self.x.rem_assign(rhs.x);
734 self.y.rem_assign(rhs.y);
735 }
736}
737
738impl Rem<i32> for IVec2 {
739 type Output = Self;
740 #[inline]
741 fn rem(self, rhs: i32) -> Self {
742 Self {
743 x: self.x.rem(rhs),
744 y: self.y.rem(rhs),
745 }
746 }
747}
748
749impl RemAssign<i32> for IVec2 {
750 #[inline]
751 fn rem_assign(&mut self, rhs: i32) {
752 self.x.rem_assign(rhs);
753 self.y.rem_assign(rhs);
754 }
755}
756
757impl Rem<IVec2> for i32 {
758 type Output = IVec2;
759 #[inline]
760 fn rem(self, rhs: IVec2) -> IVec2 {
761 IVec2 {
762 x: self.rem(rhs.x),
763 y: self.rem(rhs.y),
764 }
765 }
766}
767
768#[cfg(not(target_arch = "spirv"))]
769impl AsRef<[i32; 2]> for IVec2 {
770 #[inline]
771 fn as_ref(&self) -> &[i32; 2] {
772 unsafe { &*(self as *const IVec2 as *const [i32; 2]) }
773 }
774}
775
776#[cfg(not(target_arch = "spirv"))]
777impl AsMut<[i32; 2]> for IVec2 {
778 #[inline]
779 fn as_mut(&mut self) -> &mut [i32; 2] {
780 unsafe { &mut *(self as *mut IVec2 as *mut [i32; 2]) }
781 }
782}
783
784impl Sum for IVec2 {
785 #[inline]
786 fn sum<I>(iter: I) -> Self
787 where
788 I: Iterator<Item = Self>,
789 {
790 iter.fold(Self::ZERO, Self::add)
791 }
792}
793
794impl<'a> Sum<&'a Self> for IVec2 {
795 #[inline]
796 fn sum<I>(iter: I) -> Self
797 where
798 I: Iterator<Item = &'a Self>,
799 {
800 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
801 }
802}
803
804impl Product for IVec2 {
805 #[inline]
806 fn product<I>(iter: I) -> Self
807 where
808 I: Iterator<Item = Self>,
809 {
810 iter.fold(Self::ONE, Self::mul)
811 }
812}
813
814impl<'a> Product<&'a Self> for IVec2 {
815 #[inline]
816 fn product<I>(iter: I) -> Self
817 where
818 I: Iterator<Item = &'a Self>,
819 {
820 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
821 }
822}
823
824impl Neg for IVec2 {
825 type Output = Self;
826 #[inline]
827 fn neg(self) -> Self {
828 Self {
829 x: self.x.neg(),
830 y: self.y.neg(),
831 }
832 }
833}
834
835impl Not for IVec2 {
836 type Output = Self;
837 #[inline]
838 fn not(self) -> Self::Output {
839 Self {
840 x: self.x.not(),
841 y: self.y.not(),
842 }
843 }
844}
845
846impl BitAnd for IVec2 {
847 type Output = Self;
848 #[inline]
849 fn bitand(self, rhs: Self) -> Self::Output {
850 Self {
851 x: self.x.bitand(rhs.x),
852 y: self.y.bitand(rhs.y),
853 }
854 }
855}
856
857impl BitOr for IVec2 {
858 type Output = Self;
859 #[inline]
860 fn bitor(self, rhs: Self) -> Self::Output {
861 Self {
862 x: self.x.bitor(rhs.x),
863 y: self.y.bitor(rhs.y),
864 }
865 }
866}
867
868impl BitXor for IVec2 {
869 type Output = Self;
870 #[inline]
871 fn bitxor(self, rhs: Self) -> Self::Output {
872 Self {
873 x: self.x.bitxor(rhs.x),
874 y: self.y.bitxor(rhs.y),
875 }
876 }
877}
878
879impl BitAnd<i32> for IVec2 {
880 type Output = Self;
881 #[inline]
882 fn bitand(self, rhs: i32) -> Self::Output {
883 Self {
884 x: self.x.bitand(rhs),
885 y: self.y.bitand(rhs),
886 }
887 }
888}
889
890impl BitOr<i32> for IVec2 {
891 type Output = Self;
892 #[inline]
893 fn bitor(self, rhs: i32) -> Self::Output {
894 Self {
895 x: self.x.bitor(rhs),
896 y: self.y.bitor(rhs),
897 }
898 }
899}
900
901impl BitXor<i32> for IVec2 {
902 type Output = Self;
903 #[inline]
904 fn bitxor(self, rhs: i32) -> Self::Output {
905 Self {
906 x: self.x.bitxor(rhs),
907 y: self.y.bitxor(rhs),
908 }
909 }
910}
911
912impl Shl<i8> for IVec2 {
913 type Output = Self;
914 #[inline]
915 fn shl(self, rhs: i8) -> Self::Output {
916 Self {
917 x: self.x.shl(rhs),
918 y: self.y.shl(rhs),
919 }
920 }
921}
922
923impl Shr<i8> for IVec2 {
924 type Output = Self;
925 #[inline]
926 fn shr(self, rhs: i8) -> Self::Output {
927 Self {
928 x: self.x.shr(rhs),
929 y: self.y.shr(rhs),
930 }
931 }
932}
933
934impl Shl<i16> for IVec2 {
935 type Output = Self;
936 #[inline]
937 fn shl(self, rhs: i16) -> Self::Output {
938 Self {
939 x: self.x.shl(rhs),
940 y: self.y.shl(rhs),
941 }
942 }
943}
944
945impl Shr<i16> for IVec2 {
946 type Output = Self;
947 #[inline]
948 fn shr(self, rhs: i16) -> Self::Output {
949 Self {
950 x: self.x.shr(rhs),
951 y: self.y.shr(rhs),
952 }
953 }
954}
955
956impl Shl<i32> for IVec2 {
957 type Output = Self;
958 #[inline]
959 fn shl(self, rhs: i32) -> Self::Output {
960 Self {
961 x: self.x.shl(rhs),
962 y: self.y.shl(rhs),
963 }
964 }
965}
966
967impl Shr<i32> for IVec2 {
968 type Output = Self;
969 #[inline]
970 fn shr(self, rhs: i32) -> Self::Output {
971 Self {
972 x: self.x.shr(rhs),
973 y: self.y.shr(rhs),
974 }
975 }
976}
977
978impl Shl<i64> for IVec2 {
979 type Output = Self;
980 #[inline]
981 fn shl(self, rhs: i64) -> Self::Output {
982 Self {
983 x: self.x.shl(rhs),
984 y: self.y.shl(rhs),
985 }
986 }
987}
988
989impl Shr<i64> for IVec2 {
990 type Output = Self;
991 #[inline]
992 fn shr(self, rhs: i64) -> Self::Output {
993 Self {
994 x: self.x.shr(rhs),
995 y: self.y.shr(rhs),
996 }
997 }
998}
999
1000impl Shl<u8> for IVec2 {
1001 type Output = Self;
1002 #[inline]
1003 fn shl(self, rhs: u8) -> Self::Output {
1004 Self {
1005 x: self.x.shl(rhs),
1006 y: self.y.shl(rhs),
1007 }
1008 }
1009}
1010
1011impl Shr<u8> for IVec2 {
1012 type Output = Self;
1013 #[inline]
1014 fn shr(self, rhs: u8) -> Self::Output {
1015 Self {
1016 x: self.x.shr(rhs),
1017 y: self.y.shr(rhs),
1018 }
1019 }
1020}
1021
1022impl Shl<u16> for IVec2 {
1023 type Output = Self;
1024 #[inline]
1025 fn shl(self, rhs: u16) -> Self::Output {
1026 Self {
1027 x: self.x.shl(rhs),
1028 y: self.y.shl(rhs),
1029 }
1030 }
1031}
1032
1033impl Shr<u16> for IVec2 {
1034 type Output = Self;
1035 #[inline]
1036 fn shr(self, rhs: u16) -> Self::Output {
1037 Self {
1038 x: self.x.shr(rhs),
1039 y: self.y.shr(rhs),
1040 }
1041 }
1042}
1043
1044impl Shl<u32> for IVec2 {
1045 type Output = Self;
1046 #[inline]
1047 fn shl(self, rhs: u32) -> Self::Output {
1048 Self {
1049 x: self.x.shl(rhs),
1050 y: self.y.shl(rhs),
1051 }
1052 }
1053}
1054
1055impl Shr<u32> for IVec2 {
1056 type Output = Self;
1057 #[inline]
1058 fn shr(self, rhs: u32) -> Self::Output {
1059 Self {
1060 x: self.x.shr(rhs),
1061 y: self.y.shr(rhs),
1062 }
1063 }
1064}
1065
1066impl Shl<u64> for IVec2 {
1067 type Output = Self;
1068 #[inline]
1069 fn shl(self, rhs: u64) -> Self::Output {
1070 Self {
1071 x: self.x.shl(rhs),
1072 y: self.y.shl(rhs),
1073 }
1074 }
1075}
1076
1077impl Shr<u64> for IVec2 {
1078 type Output = Self;
1079 #[inline]
1080 fn shr(self, rhs: u64) -> Self::Output {
1081 Self {
1082 x: self.x.shr(rhs),
1083 y: self.y.shr(rhs),
1084 }
1085 }
1086}
1087
1088impl Shl<crate::IVec2> for IVec2 {
1089 type Output = Self;
1090 #[inline]
1091 fn shl(self, rhs: crate::IVec2) -> Self::Output {
1092 Self {
1093 x: self.x.shl(rhs.x),
1094 y: self.y.shl(rhs.y),
1095 }
1096 }
1097}
1098
1099impl Shr<crate::IVec2> for IVec2 {
1100 type Output = Self;
1101 #[inline]
1102 fn shr(self, rhs: crate::IVec2) -> Self::Output {
1103 Self {
1104 x: self.x.shr(rhs.x),
1105 y: self.y.shr(rhs.y),
1106 }
1107 }
1108}
1109
1110impl Shl<crate::UVec2> for IVec2 {
1111 type Output = Self;
1112 #[inline]
1113 fn shl(self, rhs: crate::UVec2) -> Self::Output {
1114 Self {
1115 x: self.x.shl(rhs.x),
1116 y: self.y.shl(rhs.y),
1117 }
1118 }
1119}
1120
1121impl Shr<crate::UVec2> for IVec2 {
1122 type Output = Self;
1123 #[inline]
1124 fn shr(self, rhs: crate::UVec2) -> Self::Output {
1125 Self {
1126 x: self.x.shr(rhs.x),
1127 y: self.y.shr(rhs.y),
1128 }
1129 }
1130}
1131
1132impl Index<usize> for IVec2 {
1133 type Output = i32;
1134 #[inline]
1135 fn index(&self, index: usize) -> &Self::Output {
1136 match index {
1137 0 => &self.x,
1138 1 => &self.y,
1139 _ => panic!("index out of bounds"),
1140 }
1141 }
1142}
1143
1144impl IndexMut<usize> for IVec2 {
1145 #[inline]
1146 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1147 match index {
1148 0 => &mut self.x,
1149 1 => &mut self.y,
1150 _ => panic!("index out of bounds"),
1151 }
1152 }
1153}
1154
1155#[cfg(not(target_arch = "spirv"))]
1156impl fmt::Display for IVec2 {
1157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1158 write!(f, "[{}, {}]", self.x, self.y)
1159 }
1160}
1161
1162#[cfg(not(target_arch = "spirv"))]
1163impl fmt::Debug for IVec2 {
1164 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1165 fmt.debug_tuple(stringify!(IVec2))
1166 .field(&self.x)
1167 .field(&self.y)
1168 .finish()
1169 }
1170}
1171
1172impl From<[i32; 2]> for IVec2 {
1173 #[inline]
1174 fn from(a: [i32; 2]) -> Self {
1175 Self::new(a[0], a[1])
1176 }
1177}
1178
1179impl From<IVec2> for [i32; 2] {
1180 #[inline]
1181 fn from(v: IVec2) -> Self {
1182 [v.x, v.y]
1183 }
1184}
1185
1186impl From<(i32, i32)> for IVec2 {
1187 #[inline]
1188 fn from(t: (i32, i32)) -> Self {
1189 Self::new(t.0, t.1)
1190 }
1191}
1192
1193impl From<IVec2> for (i32, i32) {
1194 #[inline]
1195 fn from(v: IVec2) -> Self {
1196 (v.x, v.y)
1197 }
1198}
1199
1200impl From<I16Vec2> for IVec2 {
1201 #[inline]
1202 fn from(v: I16Vec2) -> Self {
1203 Self::new(i32::from(v.x), i32::from(v.y))
1204 }
1205}
1206
1207impl From<U16Vec2> for IVec2 {
1208 #[inline]
1209 fn from(v: U16Vec2) -> Self {
1210 Self::new(i32::from(v.x), i32::from(v.y))
1211 }
1212}
1213
1214impl TryFrom<UVec2> for IVec2 {
1215 type Error = core::num::TryFromIntError;
1216
1217 #[inline]
1218 fn try_from(v: UVec2) -> Result<Self, Self::Error> {
1219 Ok(Self::new(i32::try_from(v.x)?, i32::try_from(v.y)?))
1220 }
1221}
1222
1223impl TryFrom<I64Vec2> for IVec2 {
1224 type Error = core::num::TryFromIntError;
1225
1226 #[inline]
1227 fn try_from(v: I64Vec2) -> Result<Self, Self::Error> {
1228 Ok(Self::new(i32::try_from(v.x)?, i32::try_from(v.y)?))
1229 }
1230}
1231
1232impl TryFrom<U64Vec2> for IVec2 {
1233 type Error = core::num::TryFromIntError;
1234
1235 #[inline]
1236 fn try_from(v: U64Vec2) -> Result<Self, Self::Error> {
1237 Ok(Self::new(i32::try_from(v.x)?, i32::try_from(v.y)?))
1238 }
1239}