diff options
Diffstat (limited to 'src/bool/bvec2.rs')
-rw-r--r-- | src/bool/bvec2.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/bool/bvec2.rs b/src/bool/bvec2.rs index e69fcfc..e4fe0c0 100644 --- a/src/bool/bvec2.rs +++ b/src/bool/bvec2.rs @@ -23,12 +23,14 @@ impl BVec2 { /// Creates a new vector mask. #[inline(always)] + #[must_use] pub const fn new(x: bool, y: bool) -> Self { Self { x, y } } /// Creates a vector with all elements set to `v`. #[inline] + #[must_use] pub const fn splat(v: bool) -> Self { Self::new(v, v) } @@ -38,28 +40,58 @@ impl BVec2 { /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes /// into the first lowest bit, element `y` into the second, etc. #[inline] + #[must_use] pub fn bitmask(self) -> u32 { (self.x as u32) | (self.y as u32) << 1 } /// Returns true if any of the elements are true, false otherwise. #[inline] + #[must_use] pub fn any(self) -> bool { self.x || self.y } /// Returns true if all the elements are true, false otherwise. #[inline] + #[must_use] pub fn all(self) -> bool { self.x && self.y } + /// Tests the value at `index`. + /// + /// Panics if `index` is greater than 1. + #[inline] + #[must_use] + pub fn test(&self, index: usize) -> bool { + match index { + 0 => self.x, + 1 => self.y, + _ => panic!("index out of bounds"), + } + } + + /// Sets the element at `index`. + /// + /// Panics if `index` is greater than 1. + #[inline] + pub fn set(&mut self, index: usize, value: bool) { + match index { + 0 => self.x = value, + 1 => self.y = value, + _ => panic!("index out of bounds"), + } + } + #[inline] + #[must_use] fn into_bool_array(self) -> [bool; 2] { [self.x, self.y] } #[inline] + #[must_use] fn into_u32_array(self) -> [u32; 2] { [MASK[self.x as usize], MASK[self.y as usize]] } |