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]
bvec3(x: bool, y: bool, z: bool) -> BVec39 pub const fn bvec3(x: bool, y: bool, z: bool) -> BVec3 {
10 BVec3::new(x, y, z)
11 }
12
13 /// A 3-dimensional `bool` vector mask.
14 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
15 #[repr(C, align(1))]
16 pub struct BVec3 {
17 pub x: bool,
18 pub y: bool,
19 pub z: bool,
20 }
21
22 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
23
24 impl BVec3 {
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]
new(x: bool, y: bool, z: bool) -> Self34 pub const fn new(x: bool, y: bool, z: bool) -> Self {
35 Self { x, y, z }
36 }
37
38 /// Creates a vector mask with all elements set to `v`.
39 #[inline]
40 #[must_use]
splat(v: bool) -> Self41 pub const fn splat(v: bool) -> Self {
42 Self::new(v, v, v)
43 }
44
45 /// Creates a new vector mask from a bool array.
46 #[inline]
47 #[must_use]
from_array(a: [bool; 3]) -> Self48 pub const fn from_array(a: [bool; 3]) -> Self {
49 Self::new(a[0], a[1], a[2])
50 }
51
52 /// Returns a bitmask with the lowest 3 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]
bitmask(self) -> u3258 pub fn bitmask(self) -> u32 {
59 (self.x as u32) | (self.y as u32) << 1 | (self.z as u32) << 2
60 }
61
62 /// Returns true if any of the elements are true, false otherwise.
63 #[inline]
64 #[must_use]
any(self) -> bool65 pub fn any(self) -> bool {
66 self.x || self.y || self.z
67 }
68
69 /// Returns true if all the elements are true, false otherwise.
70 #[inline]
71 #[must_use]
all(self) -> bool72 pub fn all(self) -> bool {
73 self.x && self.y && self.z
74 }
75
76 /// Tests the value at `index`.
77 ///
78 /// Panics if `index` is greater than 2.
79 #[inline]
80 #[must_use]
test(&self, index: usize) -> bool81 pub fn test(&self, index: usize) -> bool {
82 match index {
83 0 => self.x,
84 1 => self.y,
85 2 => self.z,
86 _ => panic!("index out of bounds"),
87 }
88 }
89
90 /// Sets the element at `index`.
91 ///
92 /// Panics if `index` is greater than 2.
93 #[inline]
set(&mut self, index: usize, value: bool)94 pub fn set(&mut self, index: usize, value: bool) {
95 match index {
96 0 => self.x = value,
97 1 => self.y = value,
98 2 => self.z = value,
99 _ => panic!("index out of bounds"),
100 }
101 }
102
103 #[inline]
104 #[must_use]
into_bool_array(self) -> [bool; 3]105 fn into_bool_array(self) -> [bool; 3] {
106 [self.x, self.y, self.z]
107 }
108
109 #[inline]
110 #[must_use]
into_u32_array(self) -> [u32; 3]111 fn into_u32_array(self) -> [u32; 3] {
112 [
113 MASK[self.x as usize],
114 MASK[self.y as usize],
115 MASK[self.z as usize],
116 ]
117 }
118 }
119
120 impl Default for BVec3 {
121 #[inline]
default() -> Self122 fn default() -> Self {
123 Self::FALSE
124 }
125 }
126
127 impl BitAnd for BVec3 {
128 type Output = Self;
129 #[inline]
bitand(self, rhs: Self) -> Self130 fn bitand(self, rhs: Self) -> Self {
131 Self {
132 x: self.x & rhs.x,
133 y: self.y & rhs.y,
134 z: self.z & rhs.z,
135 }
136 }
137 }
138
139 impl BitAndAssign for BVec3 {
140 #[inline]
bitand_assign(&mut self, rhs: Self)141 fn bitand_assign(&mut self, rhs: Self) {
142 *self = self.bitand(rhs);
143 }
144 }
145
146 impl BitOr for BVec3 {
147 type Output = Self;
148 #[inline]
bitor(self, rhs: Self) -> Self149 fn bitor(self, rhs: Self) -> Self {
150 Self {
151 x: self.x | rhs.x,
152 y: self.y | rhs.y,
153 z: self.z | rhs.z,
154 }
155 }
156 }
157
158 impl BitOrAssign for BVec3 {
159 #[inline]
bitor_assign(&mut self, rhs: Self)160 fn bitor_assign(&mut self, rhs: Self) {
161 *self = self.bitor(rhs);
162 }
163 }
164
165 impl BitXor for BVec3 {
166 type Output = Self;
167 #[inline]
bitxor(self, rhs: Self) -> Self168 fn bitxor(self, rhs: Self) -> Self {
169 Self {
170 x: self.x ^ rhs.x,
171 y: self.y ^ rhs.y,
172 z: self.z ^ rhs.z,
173 }
174 }
175 }
176
177 impl BitXorAssign for BVec3 {
178 #[inline]
bitxor_assign(&mut self, rhs: Self)179 fn bitxor_assign(&mut self, rhs: Self) {
180 *self = self.bitxor(rhs);
181 }
182 }
183
184 impl Not for BVec3 {
185 type Output = Self;
186 #[inline]
not(self) -> Self187 fn not(self) -> Self {
188 Self {
189 x: !self.x,
190 y: !self.y,
191 z: !self.z,
192 }
193 }
194 }
195
196 impl fmt::Debug for BVec3 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198 let arr = self.into_u32_array();
199 write!(
200 f,
201 "{}({:#x}, {:#x}, {:#x})",
202 stringify!(BVec3),
203 arr[0],
204 arr[1],
205 arr[2]
206 )
207 }
208 }
209
210 impl fmt::Display for BVec3 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result211 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 BVec3 {
218 #[inline]
from(a: [bool; 3]) -> Self219 fn from(a: [bool; 3]) -> Self {
220 Self::from_array(a)
221 }
222 }
223
224 impl From<BVec3> for [bool; 3] {
225 #[inline]
from(mask: BVec3) -> Self226 fn from(mask: BVec3) -> Self {
227 mask.into_bool_array()
228 }
229 }
230
231 impl From<BVec3> for [u32; 3] {
232 #[inline]
from(mask: BVec3) -> Self233 fn from(mask: BVec3) -> Self {
234 mask.into_u32_array()
235 }
236 }
237