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