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 2-dimensional `bool` vector mask.
7 #[inline(always)]
8 #[must_use]
bvec2(x: bool, y: bool) -> BVec29 pub 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))]
16 pub struct BVec2 {
17 pub x: bool,
18 pub y: bool,
19 }
20
21 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
22
23 impl 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]
new(x: bool, y: bool) -> Self33 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]
splat(v: bool) -> Self40 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]
from_array(a: [bool; 2]) -> Self47 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]
bitmask(self) -> u3257 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]
any(self) -> bool64 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]
all(self) -> bool71 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]
test(&self, index: usize) -> bool80 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]
set(&mut self, index: usize, value: bool)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]
into_bool_array(self) -> [bool; 2]102 fn into_bool_array(self) -> [bool; 2] {
103 [self.x, self.y]
104 }
105
106 #[inline]
107 #[must_use]
into_u32_array(self) -> [u32; 2]108 fn into_u32_array(self) -> [u32; 2] {
109 [MASK[self.x as usize], MASK[self.y as usize]]
110 }
111 }
112
113 impl Default for BVec2 {
114 #[inline]
default() -> Self115 fn default() -> Self {
116 Self::FALSE
117 }
118 }
119
120 impl BitAnd for BVec2 {
121 type Output = Self;
122 #[inline]
bitand(self, rhs: Self) -> Self123 fn bitand(self, rhs: Self) -> Self {
124 Self {
125 x: self.x & rhs.x,
126 y: self.y & rhs.y,
127 }
128 }
129 }
130
131 impl BitAndAssign for BVec2 {
132 #[inline]
bitand_assign(&mut self, rhs: Self)133 fn bitand_assign(&mut self, rhs: Self) {
134 *self = self.bitand(rhs);
135 }
136 }
137
138 impl BitOr for BVec2 {
139 type Output = Self;
140 #[inline]
bitor(self, rhs: Self) -> Self141 fn bitor(self, rhs: Self) -> Self {
142 Self {
143 x: self.x | rhs.x,
144 y: self.y | rhs.y,
145 }
146 }
147 }
148
149 impl BitOrAssign for BVec2 {
150 #[inline]
bitor_assign(&mut self, rhs: Self)151 fn bitor_assign(&mut self, rhs: Self) {
152 *self = self.bitor(rhs);
153 }
154 }
155
156 impl BitXor for BVec2 {
157 type Output = Self;
158 #[inline]
bitxor(self, rhs: Self) -> Self159 fn bitxor(self, rhs: Self) -> Self {
160 Self {
161 x: self.x ^ rhs.x,
162 y: self.y ^ rhs.y,
163 }
164 }
165 }
166
167 impl BitXorAssign for BVec2 {
168 #[inline]
bitxor_assign(&mut self, rhs: Self)169 fn bitxor_assign(&mut self, rhs: Self) {
170 *self = self.bitxor(rhs);
171 }
172 }
173
174 impl Not for BVec2 {
175 type Output = Self;
176 #[inline]
not(self) -> Self177 fn not(self) -> Self {
178 Self {
179 x: !self.x,
180 y: !self.y,
181 }
182 }
183 }
184
185 impl fmt::Debug for BVec2 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result186 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
192 impl fmt::Display for BVec2 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result193 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
199 impl From<[bool; 2]> for BVec2 {
200 #[inline]
from(a: [bool; 2]) -> Self201 fn from(a: [bool; 2]) -> Self {
202 Self::from_array(a)
203 }
204 }
205
206 impl From<BVec2> for [bool; 2] {
207 #[inline]
from(mask: BVec2) -> Self208 fn from(mask: BVec2) -> Self {
209 mask.into_bool_array()
210 }
211 }
212
213 impl From<BVec2> for [u32; 2] {
214 #[inline]
from(mask: BVec2) -> Self215 fn from(mask: BVec2) -> Self {
216 mask.into_u32_array()
217 }
218 }
219