revier_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/// Creates a 3-dimensional vector.
8#[inline(always)]
9pub 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))]
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    /// 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    pub const fn new(x: bool, y: bool, z: bool) -> Self {
34        Self { x, y, z }
35    }
36
37    /// Creates a vector with all elements set to `v`.
38    #[inline]
39    pub const fn splat(v: bool) -> Self {
40        Self::new(v, v, v)
41    }
42
43    /// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
44    ///
45    /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
46    /// into the first lowest bit, element `y` into the second, etc.
47    #[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    /// Returns true if any of the elements are true, false otherwise.
53    #[inline]
54    pub fn any(self) -> bool {
55        self.x || self.y || self.z
56    }
57
58    /// Returns true if all the elements are true, false otherwise.
59    #[inline]
60    pub fn all(self) -> bool {
61        self.x && self.y && self.z
62    }
63
64    /// Tests the value at `index`.
65    ///
66    /// Panics if `index` is greater than 2.
67    #[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    /// 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    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}