re_types/datatypes/
uvec2d_ext.rs

1use super::UVec2D;
2
3impl UVec2D {
4    /// The zero vector, i.e. the additive identity.
5    pub const ZERO: Self = Self([0; 2]);
6
7    /// The unit vector `[1, 1]`, i.e. the multiplicative identity.
8    pub const ONE: Self = Self([1; 2]);
9
10    /// Create a new vector.
11    #[inline]
12    pub const fn new(x: u32, y: u32) -> Self {
13        Self([x, y])
14    }
15
16    /// The x-coordinate, i.e. index 0.
17    #[inline]
18    pub fn x(&self) -> u32 {
19        self.0[0]
20    }
21
22    /// The y-coordinate, i.e. index 1.
23    #[inline]
24    pub fn y(&self) -> u32 {
25        self.0[1]
26    }
27
28    /// Assign a new x
29    #[inline]
30    pub fn set_x(&mut self, x: u32) {
31        self.0[0] = x;
32    }
33
34    /// Assign a new y
35    #[inline]
36    pub fn set_y(&mut self, y: u32) {
37        self.0[1] = y;
38    }
39}
40
41impl From<(u32, u32)> for UVec2D {
42    #[inline]
43    fn from((x, y): (u32, u32)) -> Self {
44        Self::new(x, y)
45    }
46}
47
48// NOTE: All these by-ref impls make the lives of end-users much easier when juggling around with
49// slices, because Rust cannot keep track of the inherent `Copy` capability of it all across all the
50// layers of `Into`/`IntoIterator`.
51
52impl<'a> From<&'a Self> for UVec2D {
53    fn from(v: &'a Self) -> Self {
54        Self(v.0)
55    }
56}
57
58impl<'a> From<&'a (u32, u32)> for UVec2D {
59    #[inline]
60    fn from((x, y): &'a (u32, u32)) -> Self {
61        Self::new(*x, *y)
62    }
63}
64
65impl<'a> From<&'a [u32; 2]> for UVec2D {
66    #[inline]
67    fn from(v: &'a [u32; 2]) -> Self {
68        Self(*v)
69    }
70}
71
72impl<Idx> std::ops::Index<Idx> for UVec2D
73where
74    Idx: std::slice::SliceIndex<[u32]>,
75{
76    type Output = Idx::Output;
77
78    #[inline]
79    fn index(&self, index: Idx) -> &Self::Output {
80        &self.0[index]
81    }
82}
83
84#[cfg(feature = "glam")]
85impl From<UVec2D> for glam::UVec2 {
86    fn from(v: UVec2D) -> Self {
87        Self::from_slice(&v.0)
88    }
89}
90
91#[cfg(feature = "glam")]
92impl From<glam::UVec2> for UVec2D {
93    fn from(v: glam::UVec2) -> Self {
94        Self(v.to_array())
95    }
96}
97
98impl std::fmt::Display for UVec2D {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        write!(f, "[{}, {}]", self.x(), self.y())
101    }
102}