glam/bool/
bvec2.rs
1#[cfg(not(target_arch = "spirv"))]
4use core::fmt;
5use core::ops::*;
6
7#[derive(Clone, Copy, PartialEq, Eq, Hash)]
9#[repr(C, align(1))]
10pub struct BVec2 {
11 pub x: bool,
12 pub y: bool,
13}
14
15const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
16
17impl BVec2 {
18 pub const FALSE: Self = Self::splat(false);
20
21 pub const TRUE: Self = Self::splat(true);
23
24 #[inline(always)]
26 #[must_use]
27 pub const fn new(x: bool, y: bool) -> Self {
28 Self { x, y }
29 }
30
31 #[inline]
33 #[must_use]
34 pub const fn splat(v: bool) -> Self {
35 Self::new(v, v)
36 }
37
38 #[inline]
43 #[must_use]
44 pub fn bitmask(self) -> u32 {
45 (self.x as u32) | (self.y as u32) << 1
46 }
47
48 #[inline]
50 #[must_use]
51 pub fn any(self) -> bool {
52 self.x || self.y
53 }
54
55 #[inline]
57 #[must_use]
58 pub fn all(self) -> bool {
59 self.x && self.y
60 }
61
62 #[inline]
66 #[must_use]
67 pub fn test(&self, index: usize) -> bool {
68 match index {
69 0 => self.x,
70 1 => self.y,
71 _ => panic!("index out of bounds"),
72 }
73 }
74
75 #[inline]
79 pub fn set(&mut self, index: usize, value: bool) {
80 match index {
81 0 => self.x = value,
82 1 => self.y = value,
83 _ => panic!("index out of bounds"),
84 }
85 }
86
87 #[inline]
88 #[must_use]
89 fn into_bool_array(self) -> [bool; 2] {
90 [self.x, self.y]
91 }
92
93 #[inline]
94 #[must_use]
95 fn into_u32_array(self) -> [u32; 2] {
96 [MASK[self.x as usize], MASK[self.y as usize]]
97 }
98}
99
100impl Default for BVec2 {
101 #[inline]
102 fn default() -> Self {
103 Self::FALSE
104 }
105}
106
107impl BitAnd for BVec2 {
108 type Output = Self;
109 #[inline]
110 fn bitand(self, rhs: Self) -> Self {
111 Self {
112 x: self.x & rhs.x,
113 y: self.y & rhs.y,
114 }
115 }
116}
117
118impl BitAndAssign for BVec2 {
119 #[inline]
120 fn bitand_assign(&mut self, rhs: Self) {
121 *self = self.bitand(rhs);
122 }
123}
124
125impl BitOr for BVec2 {
126 type Output = Self;
127 #[inline]
128 fn bitor(self, rhs: Self) -> Self {
129 Self {
130 x: self.x | rhs.x,
131 y: self.y | rhs.y,
132 }
133 }
134}
135
136impl BitOrAssign for BVec2 {
137 #[inline]
138 fn bitor_assign(&mut self, rhs: Self) {
139 *self = self.bitor(rhs);
140 }
141}
142
143impl BitXor for BVec2 {
144 type Output = Self;
145 #[inline]
146 fn bitxor(self, rhs: Self) -> Self {
147 Self {
148 x: self.x ^ rhs.x,
149 y: self.y ^ rhs.y,
150 }
151 }
152}
153
154impl BitXorAssign for BVec2 {
155 #[inline]
156 fn bitxor_assign(&mut self, rhs: Self) {
157 *self = self.bitxor(rhs);
158 }
159}
160
161impl Not for BVec2 {
162 type Output = Self;
163 #[inline]
164 fn not(self) -> Self {
165 Self {
166 x: !self.x,
167 y: !self.y,
168 }
169 }
170}
171
172#[cfg(not(target_arch = "spirv"))]
173impl fmt::Debug for BVec2 {
174 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 let arr = self.into_u32_array();
176 write!(f, "{}({:#x}, {:#x})", stringify!(BVec2), arr[0], arr[1])
177 }
178}
179
180#[cfg(not(target_arch = "spirv"))]
181impl fmt::Display for BVec2 {
182 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183 let arr = self.into_bool_array();
184 write!(f, "[{}, {}]", arr[0], arr[1])
185 }
186}
187
188impl From<BVec2> for [bool; 2] {
189 #[inline]
190 fn from(mask: BVec2) -> Self {
191 mask.into_bool_array()
192 }
193}
194
195impl From<BVec2> for [u32; 2] {
196 #[inline]
197 fn from(mask: BVec2) -> Self {
198 mask.into_u32_array()
199 }
200}