revier_glam/bool/
bvec3.rs1#[cfg(not(target_arch = "spirv"))]
4use core::fmt;
5use core::ops::*;
6
7#[inline(always)]
9pub const fn bvec3(x: bool, y: bool, z: bool) -> BVec3 {
10 BVec3::new(x, y, z)
11}
12
13#[derive(Clone, Copy, PartialEq, Eq, Hash)]
15#[repr(C, align(1))]
16pub struct BVec3 {
17 pub x: bool,
18 pub y: bool,
19 pub z: bool,
20}
21
22const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
23
24impl BVec3 {
25 pub const FALSE: Self = Self::splat(false);
27
28 pub const TRUE: Self = Self::splat(true);
30
31 #[inline(always)]
33 pub const fn new(x: bool, y: bool, z: bool) -> Self {
34 Self { x, y, z }
35 }
36
37 #[inline]
39 pub const fn splat(v: bool) -> Self {
40 Self::new(v, v, v)
41 }
42
43 #[inline]
48 pub fn bitmask(self) -> u32 {
49 (self.x as u32) | (self.y as u32) << 1 | (self.z as u32) << 2
50 }
51
52 #[inline]
54 pub fn any(self) -> bool {
55 self.x || self.y || self.z
56 }
57
58 #[inline]
60 pub fn all(self) -> bool {
61 self.x && self.y && self.z
62 }
63
64 #[inline]
68 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 #[inline]
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 fn into_bool_array(self) -> [bool; 3] {
92 [self.x, self.y, self.z]
93 }
94
95 #[inline]
96 fn into_u32_array(self) -> [u32; 3] {
97 [
98 MASK[self.x as usize],
99 MASK[self.y as usize],
100 MASK[self.z as usize],
101 ]
102 }
103}
104
105impl Default for BVec3 {
106 #[inline]
107 fn default() -> Self {
108 Self::FALSE
109 }
110}
111
112impl BitAnd for BVec3 {
113 type Output = Self;
114 #[inline]
115 fn bitand(self, rhs: Self) -> Self {
116 Self {
117 x: self.x & rhs.x,
118 y: self.y & rhs.y,
119 z: self.z & rhs.z,
120 }
121 }
122}
123
124impl BitAndAssign for BVec3 {
125 #[inline]
126 fn bitand_assign(&mut self, rhs: Self) {
127 *self = self.bitand(rhs);
128 }
129}
130
131impl BitOr for BVec3 {
132 type Output = Self;
133 #[inline]
134 fn bitor(self, rhs: Self) -> Self {
135 Self {
136 x: self.x | rhs.x,
137 y: self.y | rhs.y,
138 z: self.z | rhs.z,
139 }
140 }
141}
142
143impl BitOrAssign for BVec3 {
144 #[inline]
145 fn bitor_assign(&mut self, rhs: Self) {
146 *self = self.bitor(rhs);
147 }
148}
149
150impl BitXor for BVec3 {
151 type Output = Self;
152 #[inline]
153 fn bitxor(self, rhs: Self) -> Self {
154 Self {
155 x: self.x ^ rhs.x,
156 y: self.y ^ rhs.y,
157 z: self.z ^ rhs.z,
158 }
159 }
160}
161
162impl BitXorAssign for BVec3 {
163 #[inline]
164 fn bitxor_assign(&mut self, rhs: Self) {
165 *self = self.bitxor(rhs);
166 }
167}
168
169impl Not for BVec3 {
170 type Output = Self;
171 #[inline]
172 fn not(self) -> Self {
173 Self {
174 x: !self.x,
175 y: !self.y,
176 z: !self.z,
177 }
178 }
179}
180
181#[cfg(not(target_arch = "spirv"))]
182impl fmt::Debug for BVec3 {
183 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184 let arr = self.into_u32_array();
185 write!(
186 f,
187 "{}({:#x}, {:#x}, {:#x})",
188 stringify!(BVec3),
189 arr[0],
190 arr[1],
191 arr[2]
192 )
193 }
194}
195
196#[cfg(not(target_arch = "spirv"))]
197impl fmt::Display for BVec3 {
198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 let arr = self.into_bool_array();
200 write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
201 }
202}
203
204impl From<BVec3> for [bool; 3] {
205 #[inline]
206 fn from(mask: BVec3) -> Self {
207 mask.into_bool_array()
208 }
209}
210
211impl From<BVec3> for [u32; 3] {
212 #[inline]
213 fn from(mask: BVec3) -> Self {
214 mask.into_u32_array()
215 }
216}