1use core::fmt;
4use core::ops::*;
5
6#[inline(always)]
8#[must_use]
9pub const fn bvec2(x: bool, y: bool) -> BVec2 {
10 BVec2::new(x, y)
11}
12
13#[derive(Clone, Copy, PartialEq, Eq, Hash)]
15#[repr(C, align(1))]
16#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
17pub struct BVec2 {
18 pub x: bool,
19 pub y: bool,
20}
21
22const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
23
24impl BVec2 {
25 pub const FALSE: Self = Self::splat(false);
27
28 pub const TRUE: Self = Self::splat(true);
30
31 #[inline(always)]
33 #[must_use]
34 pub const fn new(x: bool, y: bool) -> Self {
35 Self { x, y }
36 }
37
38 #[inline]
40 #[must_use]
41 pub const fn splat(v: bool) -> Self {
42 Self::new(v, v)
43 }
44
45 #[inline]
47 #[must_use]
48 pub const fn from_array(a: [bool; 2]) -> Self {
49 Self::new(a[0], a[1])
50 }
51
52 #[inline]
57 #[must_use]
58 pub fn bitmask(self) -> u32 {
59 (self.x as u32) | ((self.y as u32) << 1)
60 }
61
62 #[inline]
64 #[must_use]
65 pub fn any(self) -> bool {
66 self.x || self.y
67 }
68
69 #[inline]
71 #[must_use]
72 pub fn all(self) -> bool {
73 self.x && self.y
74 }
75
76 #[inline]
80 #[must_use]
81 pub fn test(&self, index: usize) -> bool {
82 match index {
83 0 => self.x,
84 1 => self.y,
85 _ => panic!("index out of bounds"),
86 }
87 }
88
89 #[inline]
93 pub fn set(&mut self, index: usize, value: bool) {
94 match index {
95 0 => self.x = value,
96 1 => self.y = value,
97 _ => panic!("index out of bounds"),
98 }
99 }
100
101 #[inline]
102 #[must_use]
103 fn into_bool_array(self) -> [bool; 2] {
104 [self.x, self.y]
105 }
106
107 #[inline]
108 #[must_use]
109 fn into_u32_array(self) -> [u32; 2] {
110 [MASK[self.x as usize], MASK[self.y as usize]]
111 }
112}
113
114impl Default for BVec2 {
115 #[inline]
116 fn default() -> Self {
117 Self::FALSE
118 }
119}
120
121impl BitAnd for BVec2 {
122 type Output = Self;
123 #[inline]
124 fn bitand(self, rhs: Self) -> Self {
125 Self {
126 x: self.x & rhs.x,
127 y: self.y & rhs.y,
128 }
129 }
130}
131
132impl BitAnd<&Self> for BVec2 {
133 type Output = Self;
134 #[inline]
135 fn bitand(self, rhs: &Self) -> Self {
136 self.bitand(*rhs)
137 }
138}
139
140impl BitAnd<&BVec2> for &BVec2 {
141 type Output = BVec2;
142 #[inline]
143 fn bitand(self, rhs: &BVec2) -> BVec2 {
144 (*self).bitand(*rhs)
145 }
146}
147
148impl BitAnd<BVec2> for &BVec2 {
149 type Output = BVec2;
150 #[inline]
151 fn bitand(self, rhs: BVec2) -> BVec2 {
152 (*self).bitand(rhs)
153 }
154}
155
156impl BitAndAssign for BVec2 {
157 #[inline]
158 fn bitand_assign(&mut self, rhs: Self) {
159 *self = self.bitand(rhs);
160 }
161}
162
163impl BitAndAssign<&Self> for BVec2 {
164 #[inline]
165 fn bitand_assign(&mut self, rhs: &Self) {
166 self.bitand_assign(*rhs);
167 }
168}
169
170impl BitOr for BVec2 {
171 type Output = Self;
172 #[inline]
173 fn bitor(self, rhs: Self) -> Self {
174 Self {
175 x: self.x | rhs.x,
176 y: self.y | rhs.y,
177 }
178 }
179}
180
181impl BitOr<&Self> for BVec2 {
182 type Output = Self;
183 #[inline]
184 fn bitor(self, rhs: &Self) -> Self {
185 self.bitor(*rhs)
186 }
187}
188
189impl BitOr<&BVec2> for &BVec2 {
190 type Output = BVec2;
191 #[inline]
192 fn bitor(self, rhs: &BVec2) -> BVec2 {
193 (*self).bitor(*rhs)
194 }
195}
196
197impl BitOr<BVec2> for &BVec2 {
198 type Output = BVec2;
199 #[inline]
200 fn bitor(self, rhs: BVec2) -> BVec2 {
201 (*self).bitor(rhs)
202 }
203}
204
205impl BitOrAssign for BVec2 {
206 #[inline]
207 fn bitor_assign(&mut self, rhs: Self) {
208 *self = self.bitor(rhs);
209 }
210}
211
212impl BitOrAssign<&Self> for BVec2 {
213 #[inline]
214 fn bitor_assign(&mut self, rhs: &Self) {
215 self.bitor_assign(*rhs);
216 }
217}
218
219impl BitXor for BVec2 {
220 type Output = Self;
221 #[inline]
222 fn bitxor(self, rhs: Self) -> Self {
223 Self {
224 x: self.x ^ rhs.x,
225 y: self.y ^ rhs.y,
226 }
227 }
228}
229
230impl BitXor<&Self> for BVec2 {
231 type Output = Self;
232 #[inline]
233 fn bitxor(self, rhs: &Self) -> Self {
234 self.bitxor(*rhs)
235 }
236}
237
238impl BitXor<&BVec2> for &BVec2 {
239 type Output = BVec2;
240 #[inline]
241 fn bitxor(self, rhs: &BVec2) -> BVec2 {
242 (*self).bitxor(*rhs)
243 }
244}
245
246impl BitXor<BVec2> for &BVec2 {
247 type Output = BVec2;
248 #[inline]
249 fn bitxor(self, rhs: BVec2) -> BVec2 {
250 (*self).bitxor(rhs)
251 }
252}
253
254impl BitXorAssign for BVec2 {
255 #[inline]
256 fn bitxor_assign(&mut self, rhs: Self) {
257 *self = self.bitxor(rhs);
258 }
259}
260
261impl BitXorAssign<&Self> for BVec2 {
262 #[inline]
263 fn bitxor_assign(&mut self, rhs: &Self) {
264 self.bitxor_assign(*rhs);
265 }
266}
267
268impl Not for BVec2 {
269 type Output = Self;
270 #[inline]
271 fn not(self) -> Self {
272 Self {
273 x: !self.x,
274 y: !self.y,
275 }
276 }
277}
278
279impl Not for &BVec2 {
280 type Output = BVec2;
281 #[inline]
282 fn not(self) -> BVec2 {
283 (*self).not()
284 }
285}
286
287impl fmt::Debug for BVec2 {
288 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
289 let arr = self.into_u32_array();
290 write!(f, "{}({:#x}, {:#x})", stringify!(BVec2), arr[0], arr[1])
291 }
292}
293
294impl fmt::Display for BVec2 {
295 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296 let arr = self.into_bool_array();
297 write!(f, "[{}, {}]", arr[0], arr[1])
298 }
299}
300
301impl From<[bool; 2]> for BVec2 {
302 #[inline]
303 fn from(a: [bool; 2]) -> Self {
304 Self::from_array(a)
305 }
306}
307
308impl From<BVec2> for [bool; 2] {
309 #[inline]
310 fn from(mask: BVec2) -> Self {
311 mask.into_bool_array()
312 }
313}
314
315impl From<BVec2> for [u32; 2] {
316 #[inline]
317 fn from(mask: BVec2) -> Self {
318 mask.into_u32_array()
319 }
320}