async_ecs/misc/bit/
and.rs

1use hibitset::{BitSetLike, DrainableBitSet};
2
3use crate::{entity::Index, misc::Split};
4
5/* BitSetAnd */
6
7#[derive(Debug, Clone, Copy)]
8pub struct BitSetAnd<A: BitSetLike, B: BitSetLike>(pub A, pub B);
9
10impl<A, B> BitSetLike for BitSetAnd<A, B>
11where
12    A: BitSetLike,
13    B: BitSetLike,
14{
15    #[inline]
16    fn layer3(&self) -> usize {
17        self.0.layer3() & self.1.layer3()
18    }
19    #[inline]
20    fn layer2(&self, i: usize) -> usize {
21        self.0.layer2(i) & self.1.layer2(i)
22    }
23    #[inline]
24    fn layer1(&self, i: usize) -> usize {
25        self.0.layer1(i) & self.1.layer1(i)
26    }
27    #[inline]
28    fn layer0(&self, i: usize) -> usize {
29        self.0.layer0(i) & self.1.layer0(i)
30    }
31    #[inline]
32    fn contains(&self, i: Index) -> bool {
33        self.0.contains(i) && self.1.contains(i)
34    }
35}
36
37impl<A, B> DrainableBitSet for BitSetAnd<A, B>
38where
39    A: DrainableBitSet,
40    B: DrainableBitSet,
41{
42    #[inline]
43    fn remove(&mut self, i: Index) -> bool {
44        if self.contains(i) {
45            self.0.remove(i);
46            self.1.remove(i);
47            true
48        } else {
49            false
50        }
51    }
52}
53
54/* BitAnd */
55
56pub trait BitAnd {
57    type Value: BitSetLike;
58
59    fn and(self) -> Self::Value;
60}
61
62impl<A> BitAnd for (A,)
63where
64    A: BitSetLike,
65{
66    type Value = A;
67
68    fn and(self) -> Self::Value {
69        self.0
70    }
71}
72
73macro_rules! bitset_and {
74    ($($from:ident),*) => {
75        impl<$($from),*> BitAnd for ($($from),*)
76            where $($from: BitSetLike),*
77        {
78            type Value = BitSetAnd<
79                <<Self as Split>::Left as BitAnd>::Value,
80                <<Self as Split>::Right as BitAnd>::Value
81            >;
82
83            fn and(self) -> Self::Value {
84                let (l, r) = self.split();
85
86                BitSetAnd(l.and(), r.and())
87            }
88        }
89    }
90}
91
92bitset_and! { A, B }
93bitset_and! { A, B, C }
94bitset_and! { A, B, C, D }
95bitset_and! { A, B, C, D, E }
96bitset_and! { A, B, C, D, E, F }
97bitset_and! { A, B, C, D, E, F, G }
98bitset_and! { A, B, C, D, E, F, G, H }
99bitset_and! { A, B, C, D, E, F, G, H, I }
100bitset_and! { A, B, C, D, E, F, G, H, I, J }
101bitset_and! { A, B, C, D, E, F, G, H, I, J, K }
102bitset_and! { A, B, C, D, E, F, G, H, I, J, K, L }
103bitset_and! { A, B, C, D, E, F, G, H, I, J, K, L, M }
104bitset_and! { A, B, C, D, E, F, G, H, I, J, K, L, M, N }
105bitset_and! { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O }
106bitset_and! { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P }