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))]
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    #[must_use]
33    pub const fn new(x: bool, y: bool) -> Self {
34        Self { x, y }
35    }
36
37    /// Creates a vector mask with all elements set to `v`.
38    #[inline]
39    #[must_use]
40    pub const fn splat(v: bool) -> Self {
41        Self::new(v, v)
42    }
43
44    /// Creates a new vector mask from a bool array.
45    #[inline]
46    #[must_use]
47    pub const fn from_array(a: [bool; 2]) -> Self {
48        Self::new(a[0], a[1])
49    }
50
51    /// Returns a bitmask with the lowest 2 bits set from the elements of `self`.
52    ///
53    /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
54    /// into the first lowest bit, element `y` into the second, etc.
55    #[inline]
56    #[must_use]
57    pub fn bitmask(self) -> u32 {
58        (self.x as u32) | (self.y as u32) << 1
59    }
60
61    /// Returns true if any of the elements are true, false otherwise.
62    #[inline]
63    #[must_use]
64    pub fn any(self) -> bool {
65        self.x || self.y
66    }
67
68    /// Returns true if all the elements are true, false otherwise.
69    #[inline]
70    #[must_use]
71    pub fn all(self) -> bool {
72        self.x && self.y
73    }
74
75    /// Tests the value at `index`.
76    ///
77    /// Panics if `index` is greater than 1.
78    #[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    /// Sets the element at `index`.
89    ///
90    /// Panics if `index` is greater than 1.
91    #[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 BitAndAssign for BVec2 {
132    #[inline]
133    fn bitand_assign(&mut self, rhs: Self) {
134        *self = self.bitand(rhs);
135    }
136}
137
138impl BitOr for BVec2 {
139    type Output = Self;
140    #[inline]
141    fn bitor(self, rhs: Self) -> Self {
142        Self {
143            x: self.x | rhs.x,
144            y: self.y | rhs.y,
145        }
146    }
147}
148
149impl BitOrAssign for BVec2 {
150    #[inline]
151    fn bitor_assign(&mut self, rhs: Self) {
152        *self = self.bitor(rhs);
153    }
154}
155
156impl BitXor for BVec2 {
157    type Output = Self;
158    #[inline]
159    fn bitxor(self, rhs: Self) -> Self {
160        Self {
161            x: self.x ^ rhs.x,
162            y: self.y ^ rhs.y,
163        }
164    }
165}
166
167impl BitXorAssign for BVec2 {
168    #[inline]
169    fn bitxor_assign(&mut self, rhs: Self) {
170        *self = self.bitxor(rhs);
171    }
172}
173
174impl Not for BVec2 {
175    type Output = Self;
176    #[inline]
177    fn not(self) -> Self {
178        Self {
179            x: !self.x,
180            y: !self.y,
181        }
182    }
183}
184
185impl fmt::Debug for BVec2 {
186    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187        let arr = self.into_u32_array();
188        write!(f, "{}({:#x}, {:#x})", stringify!(BVec2), arr[0], arr[1])
189    }
190}
191
192impl fmt::Display for BVec2 {
193    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194        let arr = self.into_bool_array();
195        write!(f, "[{}, {}]", arr[0], arr[1])
196    }
197}
198
199impl From<[bool; 2]> for BVec2 {
200    #[inline]
201    fn from(a: [bool; 2]) -> Self {
202        Self::from_array(a)
203    }
204}
205
206impl From<BVec2> for [bool; 2] {
207    #[inline]
208    fn from(mask: BVec2) -> Self {
209        mask.into_bool_array()
210    }
211}
212
213impl From<BVec2> for [u32; 2] {
214    #[inline]
215    fn from(mask: BVec2) -> Self {
216        mask.into_u32_array()
217    }
218}