glam/bool/
bvec3.rs

1// Generated from vec_mask.rs.tera template. Edit the template, not the generated file.
2
3#[cfg(not(target_arch = "spirv"))]
4use core::fmt;
5use core::ops::*;
6
7/// A 3-dimensional `bool` vector mask.
8#[derive(Clone, Copy, PartialEq, Eq, Hash)]
9#[repr(C, align(1))]
10pub struct BVec3 {
11    pub x: bool,
12    pub y: bool,
13    pub z: bool,
14}
15
16const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
17
18impl 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]
28    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]
35    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]
45    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]
52    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]
59    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]
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    /// Sets the element at `index`.
78    ///
79    /// Panics if `index` is greater than 2.
80    #[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    #[must_use]
92    fn into_bool_array(self) -> [bool; 3] {
93        [self.x, self.y, self.z]
94    }
95
96    #[inline]
97    #[must_use]
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
107impl Default for BVec3 {
108    #[inline]
109    fn default() -> Self {
110        Self::FALSE
111    }
112}
113
114impl BitAnd for BVec3 {
115    type Output = Self;
116    #[inline]
117    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
126impl BitAndAssign for BVec3 {
127    #[inline]
128    fn bitand_assign(&mut self, rhs: Self) {
129        *self = self.bitand(rhs);
130    }
131}
132
133impl BitOr for BVec3 {
134    type Output = Self;
135    #[inline]
136    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
145impl BitOrAssign for BVec3 {
146    #[inline]
147    fn bitor_assign(&mut self, rhs: Self) {
148        *self = self.bitor(rhs);
149    }
150}
151
152impl BitXor for BVec3 {
153    type Output = Self;
154    #[inline]
155    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
164impl BitXorAssign for BVec3 {
165    #[inline]
166    fn bitxor_assign(&mut self, rhs: Self) {
167        *self = self.bitxor(rhs);
168    }
169}
170
171impl Not for BVec3 {
172    type Output = Self;
173    #[inline]
174    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"))]
184impl fmt::Debug for BVec3 {
185    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"))]
199impl fmt::Display for BVec3 {
200    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
206impl From<BVec3> for [bool; 3] {
207    #[inline]
208    fn from(mask: BVec3) -> Self {
209        mask.into_bool_array()
210    }
211}
212
213impl From<BVec3> for [u32; 3] {
214    #[inline]
215    fn from(mask: BVec3) -> Self {
216        mask.into_u32_array()
217    }
218}