Debian Codegraph @

debian/missing-sources/glam/src/bool/scalar/bvec3a.rs

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