Skip to main content

glam/bool/
bvec2.rs

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