Debian Codegraph @

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

raw
1 // Generated from vec_mask.rs.tera template. Edit the template, not the generated file.
2
3 #[cfg(not(target_arch = "spirv"))]
4 use core::fmt;
5 use core::ops::*;
6
7 /// A 3-dimensional `u32` vector mask.
8 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
9 #[repr(C, align(16))]
10 pub struct BVec3A {
11 pub x: u32,
12 pub y: u32,
13 pub z: u32,
14 }
15
16 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
17
18 impl BVec3A {
19 /// All false.
20 pub const FALSE: Self = Self::splat(false);
21
22 /// All true.
23 pub const TRUE: Self = Self::splat(true);
24
25 /// Creates a new vector mask.
26 #[inline(always)]
27 #[must_use]
28 pub const fn new(x: bool, y: bool, z: bool) -> Self {
29 Self {
30 x: MASK[x as usize],
31 y: MASK[y as usize],
32 z: MASK[z as usize],
33 }
34 }
35
36 /// Creates a vector mask with all elements set to `v`.
37 #[inline]
38 #[must_use]
39 pub const fn splat(v: bool) -> Self {
40 Self::new(v, v, v)
41 }
42
43 /// Creates a new vector mask from a bool array.
44 #[inline]
45 #[must_use]
46 pub const fn from_array(a: [bool; 3]) -> Self {
47 Self::new(a[0], a[1], a[2])
48 }
49
50 /// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
51 ///
52 /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
53 /// into the first lowest bit, element `y` into the second, etc.
54 #[inline]
55 #[must_use]
56 pub fn bitmask(self) -> u32 {
57 (self.x & 0x1) | (self.y & 0x1) << 1 | (self.z & 0x1) << 2
58 }
59
60 /// Returns true if any of the elements are true, false otherwise.
61 #[inline]
62 #[must_use]
63 pub fn any(self) -> bool {
64 ((self.x | self.y | self.z) & 0x1) != 0
65 }
66
67 /// Returns true if all the elements are true, false otherwise.
68 #[inline]
69 #[must_use]
70 pub fn all(self) -> bool {
71 ((self.x & self.y & self.z) & 0x1) != 0
72 }
73
74 /// Tests the value at `index`.
75 ///
76 /// Panics if `index` is greater than 2.
77 #[inline]
78 #[must_use]
79 pub fn test(&self, index: usize) -> bool {
80 match index {
81 0 => (self.x & 0x1) != 0,
82 1 => (self.y & 0x1) != 0,
83 2 => (self.z & 0x1) != 0,
84 _ => panic!("index out of bounds"),
85 }
86 }
87
88 /// Sets the element at `index`.
89 ///
90 /// Panics if `index` is greater than 2.
91 #[inline]
92 pub fn set(&mut self, index: usize, value: bool) {
93 match index {
94 0 => self.x = MASK[value as usize],
95 1 => self.y = MASK[value as usize],
96 2 => self.z = MASK[value as usize],
97 _ => panic!("index out of bounds"),
98 }
99 }
100
101 #[inline]
102 #[must_use]
103 fn into_bool_array(self) -> [bool; 3] {
104 [
105 (self.x & 0x1) != 0,
106 (self.y & 0x1) != 0,
107 (self.z & 0x1) != 0,
108 ]
109 }
110
111 #[inline]
112 #[must_use]
113 fn into_u32_array(self) -> [u32; 3] {
114 [self.x, self.y, self.z]
115 }
116 }
117
118 impl Default for BVec3A {
119 #[inline]
120 fn default() -> Self {
121 Self::FALSE
122 }
123 }
124
125 impl BitAnd for BVec3A {
126 type Output = Self;
127 #[inline]
128 fn bitand(self, rhs: Self) -> Self {
129 Self {
130 x: self.x & rhs.x,
131 y: self.y & rhs.y,
132 z: self.z & rhs.z,
133 }
134 }
135 }
136
137 impl BitAndAssign for BVec3A {
138 #[inline]
139 fn bitand_assign(&mut self, rhs: Self) {
140 *self = self.bitand(rhs);
141 }
142 }
143
144 impl BitOr for BVec3A {
145 type Output = Self;
146 #[inline]
147 fn bitor(self, rhs: Self) -> Self {
148 Self {
149 x: self.x | rhs.x,
150 y: self.y | rhs.y,
151 z: self.z | rhs.z,
152 }
153 }
154 }
155
156 impl BitOrAssign for BVec3A {
157 #[inline]
158 fn bitor_assign(&mut self, rhs: Self) {
159 *self = self.bitor(rhs);
160 }
161 }
162
163 impl BitXor for BVec3A {
164 type Output = Self;
165 #[inline]
166 fn bitxor(self, rhs: Self) -> Self {
167 Self {
168 x: self.x ^ rhs.x,
169 y: self.y ^ rhs.y,
170 z: self.z ^ rhs.z,
171 }
172 }
173 }
174
175 impl BitXorAssign for BVec3A {
176 #[inline]
177 fn bitxor_assign(&mut self, rhs: Self) {
178 *self = self.bitxor(rhs);
179 }
180 }
181
182 impl Not for BVec3A {
183 type Output = Self;
184 #[inline]
185 fn not(self) -> Self {
186 Self {
187 x: !self.x,
188 y: !self.y,
189 z: !self.z,
190 }
191 }
192 }
193
194 #[cfg(not(target_arch = "spirv"))]
195 impl fmt::Debug for BVec3A {
196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197 let arr = self.into_u32_array();
198 write!(
199 f,
200 "{}({:#x}, {:#x}, {:#x})",
201 stringify!(BVec3A),
202 arr[0],
203 arr[1],
204 arr[2]
205 )
206 }
207 }
208
209 #[cfg(not(target_arch = "spirv"))]
210 impl fmt::Display for BVec3A {
211 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212 let arr = self.into_bool_array();
213 write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
214 }
215 }
216
217 impl From<[bool; 3]> for BVec3A {
218 #[inline]
219 fn from(a: [bool; 3]) -> Self {
220 Self::from_array(a)
221 }
222 }
223
224 impl From<BVec3A> for [bool; 3] {
225 #[inline]
226 fn from(mask: BVec3A) -> Self {
227 mask.into_bool_array()
228 }
229 }
230
231 impl From<BVec3A> for [u32; 3] {
232 #[inline]
233 fn from(mask: BVec3A) -> Self {
234 mask.into_u32_array()
235 }
236 }