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