emath/
vec2b.rs

1/// Two bools, one for each axis (X and Y).
2#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
3#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
4pub struct Vec2b {
5    pub x: bool,
6    pub y: bool,
7}
8
9impl Vec2b {
10    pub const FALSE: Self = Self { x: false, y: false };
11    pub const TRUE: Self = Self { x: true, y: true };
12
13    #[inline]
14    pub fn new(x: bool, y: bool) -> Self {
15        Self { x, y }
16    }
17
18    #[inline]
19    pub fn any(&self) -> bool {
20        self.x || self.y
21    }
22
23    /// Are both `x` and `y` true?
24    #[inline]
25    pub fn all(&self) -> bool {
26        self.x && self.y
27    }
28
29    #[inline]
30    pub fn and(&self, other: impl Into<Self>) -> Self {
31        let other = other.into();
32        Self {
33            x: self.x && other.x,
34            y: self.y && other.y,
35        }
36    }
37
38    #[inline]
39    pub fn or(&self, other: impl Into<Self>) -> Self {
40        let other = other.into();
41        Self {
42            x: self.x || other.x,
43            y: self.y || other.y,
44        }
45    }
46}
47
48impl From<bool> for Vec2b {
49    #[inline]
50    fn from(val: bool) -> Self {
51        Self { x: val, y: val }
52    }
53}
54
55impl From<[bool; 2]> for Vec2b {
56    #[inline]
57    fn from([x, y]: [bool; 2]) -> Self {
58        Self { x, y }
59    }
60}
61
62impl std::ops::Index<usize> for Vec2b {
63    type Output = bool;
64
65    #[inline(always)]
66    fn index(&self, index: usize) -> &bool {
67        match index {
68            0 => &self.x,
69            1 => &self.y,
70            _ => panic!("Vec2b index out of bounds: {index}"),
71        }
72    }
73}
74
75impl std::ops::IndexMut<usize> for Vec2b {
76    #[inline(always)]
77    fn index_mut(&mut self, index: usize) -> &mut bool {
78        match index {
79            0 => &mut self.x,
80            1 => &mut self.y,
81            _ => panic!("Vec2b index out of bounds: {index}"),
82        }
83    }
84}
85
86impl std::ops::Not for Vec2b {
87    type Output = Self;
88
89    #[inline]
90    fn not(self) -> Self::Output {
91        Self {
92            x: !self.x,
93            y: !self.y,
94        }
95    }
96}