revier_glam/bool/
bvec2.rs

1// Generated from vec_mask.rs.tera template. Edit the template, not the generated file.
2
3#[cfg(not(target_arch = "spirv"))]
4use core::fmt;
5use core::ops::*;
6
7/// Creates a 2-dimensional vector.
8#[inline(always)]
9pub const fn bvec2(x: bool, y: bool) -> BVec2 {
10    BVec2::new(x, y)
11}
12
13/// A 2-dimensional `bool` vector mask.
14#[derive(Clone, Copy, PartialEq, Eq, Hash)]
15#[repr(C, align(1))]
16pub struct BVec2 {
17    pub x: bool,
18    pub y: bool,
19}
20
21const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
22
23impl BVec2 {
24    /// All false.
25    pub const FALSE: Self = Self::splat(false);
26
27    /// All true.
28    pub const TRUE: Self = Self::splat(true);
29
30    /// Creates a new vector mask.
31    #[inline(always)]
32    pub const fn new(x: bool, y: bool) -> Self {
33        Self { x, y }
34    }
35
36    /// Creates a vector with all elements set to `v`.
37    #[inline]
38    pub const fn splat(v: bool) -> Self {
39        Self::new(v, v)
40    }
41
42    /// Returns a bitmask with the lowest 2 bits set from the elements of `self`.
43    ///
44    /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
45    /// into the first lowest bit, element `y` into the second, etc.
46    #[inline]
47    pub fn bitmask(self) -> u32 {
48        (self.x as u32) | (self.y as u32) << 1
49    }
50
51    /// Returns true if any of the elements are true, false otherwise.
52    #[inline]
53    pub fn any(self) -> bool {
54        self.x || self.y
55    }
56
57    /// Returns true if all the elements are true, false otherwise.
58    #[inline]
59    pub fn all(self) -> bool {
60        self.x && self.y
61    }
62
63    /// Tests the value at `index`.
64    ///
65    /// Panics if `index` is greater than 1.
66    #[inline]
67    pub fn test(&self, index: usize) -> bool {
68        match index {
69            0 => self.x,
70            1 => self.y,
71            _ => panic!("index out of bounds"),
72        }
73    }
74
75    /// Sets the element at `index`.
76    ///
77    /// Panics if `index` is greater than 1.
78    #[inline]
79    pub fn set(&mut self, index: usize, value: bool) {
80        match index {
81            0 => self.x = value,
82            1 => self.y = value,
83            _ => panic!("index out of bounds"),
84        }
85    }
86
87    #[inline]
88    fn into_bool_array(self) -> [bool; 2] {
89        [self.x, self.y]
90    }
91
92    #[inline]
93    fn into_u32_array(self) -> [u32; 2] {
94        [MASK[self.x as usize], MASK[self.y as usize]]
95    }
96}
97
98impl Default for BVec2 {
99    #[inline]
100    fn default() -> Self {
101        Self::FALSE
102    }
103}
104
105impl BitAnd for BVec2 {
106    type Output = Self;
107    #[inline]
108    fn bitand(self, rhs: Self) -> Self {
109        Self {
110            x: self.x & rhs.x,
111            y: self.y & rhs.y,
112        }
113    }
114}
115
116impl BitAndAssign for BVec2 {
117    #[inline]
118    fn bitand_assign(&mut self, rhs: Self) {
119        *self = self.bitand(rhs);
120    }
121}
122
123impl BitOr for BVec2 {
124    type Output = Self;
125    #[inline]
126    fn bitor(self, rhs: Self) -> Self {
127        Self {
128            x: self.x | rhs.x,
129            y: self.y | rhs.y,
130        }
131    }
132}
133
134impl BitOrAssign for BVec2 {
135    #[inline]
136    fn bitor_assign(&mut self, rhs: Self) {
137        *self = self.bitor(rhs);
138    }
139}
140
141impl BitXor for BVec2 {
142    type Output = Self;
143    #[inline]
144    fn bitxor(self, rhs: Self) -> Self {
145        Self {
146            x: self.x ^ rhs.x,
147            y: self.y ^ rhs.y,
148        }
149    }
150}
151
152impl BitXorAssign for BVec2 {
153    #[inline]
154    fn bitxor_assign(&mut self, rhs: Self) {
155        *self = self.bitxor(rhs);
156    }
157}
158
159impl Not for BVec2 {
160    type Output = Self;
161    #[inline]
162    fn not(self) -> Self {
163        Self {
164            x: !self.x,
165            y: !self.y,
166        }
167    }
168}
169
170#[cfg(not(target_arch = "spirv"))]
171impl fmt::Debug for BVec2 {
172    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173        let arr = self.into_u32_array();
174        write!(f, "{}({:#x}, {:#x})", stringify!(BVec2), arr[0], arr[1])
175    }
176}
177
178#[cfg(not(target_arch = "spirv"))]
179impl fmt::Display for BVec2 {
180    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181        let arr = self.into_bool_array();
182        write!(f, "[{}, {}]", arr[0], arr[1])
183    }
184}
185
186impl From<BVec2> for [bool; 2] {
187    #[inline]
188    fn from(mask: BVec2) -> Self {
189        mask.into_bool_array()
190    }
191}
192
193impl From<BVec2> for [u32; 2] {
194    #[inline]
195    fn from(mask: BVec2) -> Self {
196        mask.into_u32_array()
197    }
198}