1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use super::UVec2D;

impl UVec2D {
    /// The zero vector, i.e. the additive identity.
    pub const ZERO: Self = Self([0; 2]);

    /// The unit vector `[1, 1]`, i.e. the multiplicative identity.
    pub const ONE: Self = Self([1; 2]);

    /// Create a new vector.
    #[inline]
    pub const fn new(x: u32, y: u32) -> Self {
        Self([x, y])
    }

    /// The x-coordinate, i.e. index 0.
    #[inline]
    pub fn x(&self) -> u32 {
        self.0[0]
    }

    /// The y-coordinate, i.e. index 1.
    #[inline]
    pub fn y(&self) -> u32 {
        self.0[1]
    }
}

impl From<(u32, u32)> for UVec2D {
    #[inline]
    fn from((x, y): (u32, u32)) -> Self {
        Self::new(x, y)
    }
}

// NOTE: All these by-ref impls make the lives of end-users much easier when juggling around with
// slices, because Rust cannot keep track of the inherent `Copy` capability of it all across all the
// layers of `Into`/`IntoIterator`.

impl<'a> From<&'a UVec2D> for UVec2D {
    fn from(v: &'a UVec2D) -> Self {
        Self(v.0)
    }
}

impl<'a> From<&'a (u32, u32)> for UVec2D {
    #[inline]
    fn from((x, y): &'a (u32, u32)) -> Self {
        Self::new(*x, *y)
    }
}

impl<'a> From<&'a [u32; 2]> for UVec2D {
    #[inline]
    fn from(v: &'a [u32; 2]) -> Self {
        Self(*v)
    }
}

impl<Idx> std::ops::Index<Idx> for UVec2D
where
    Idx: std::slice::SliceIndex<[u32]>,
{
    type Output = Idx::Output;

    #[inline]
    fn index(&self, index: Idx) -> &Self::Output {
        &self.0[index]
    }
}

#[cfg(feature = "glam")]
impl From<UVec2D> for glam::UVec2 {
    fn from(v: UVec2D) -> Self {
        Self::from_slice(&v.0)
    }
}

#[cfg(feature = "glam")]
impl From<glam::UVec2> for UVec2D {
    fn from(v: glam::UVec2) -> Self {
        Self(v.to_array())
    }
}

impl std::fmt::Display for UVec2D {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}, {}]", self.x(), self.y())
    }
}