1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use super::{
    not::Not, or::Or, passthrough::Passthrough, ActiveFilter, DynamicFilter, FilterResult,
    GroupMatcher, LayoutFilter,
};
use crate::internals::{query::view::Fetch, storage::component::ComponentTypeId, world::WorldId};

/// A filter which requires all filters within `T` match.
#[derive(Debug, Clone)]
pub struct And<T> {
    pub(super) filters: T,
}

macro_rules! and_filter {
    ($head_ty:ident) => {
        impl_and_filter!($head_ty);
    };
    ($head_ty:ident, $( $tail_ty:ident ),*) => (
        impl_and_filter!($head_ty, $( $tail_ty ),*);
        and_filter!($( $tail_ty ),*);
    );
}

macro_rules! impl_and_filter {
    ( $( $ty:ident ),* ) => {
        impl<$( $ty ),*> ActiveFilter for And<($( $ty, )*)> {}

        impl<$( $ty: Default ),*> Default for And<($( $ty, )*)> {
            fn default() -> Self {
                Self {
                    filters: ($( $ty::default(), )*)
                }
            }
        }

        impl<$( $ty: GroupMatcher ),*> GroupMatcher for And<($( $ty, )*)> {
            fn can_match_group() -> bool {
                $( $ty::can_match_group() )&&*
            }
            fn group_components() -> Vec<ComponentTypeId> {
                let mut components = Vec::new();
                $( components.extend($ty::group_components()); )*
                components
            }
        }

        impl<$( $ty: LayoutFilter ),*> LayoutFilter for And<($( $ty, )*)> {
            #[inline]
            fn matches_layout(&self, components: &[ComponentTypeId]) -> FilterResult {
                #![allow(non_snake_case)]
                let ($( $ty, )*) = &self.filters;
                let mut result = FilterResult::Defer;
                $( result = result.coalesce_and($ty.matches_layout(components)); )*
                result
            }
        }

        impl<$( $ty: DynamicFilter ),*> DynamicFilter for And<($( $ty, )*)> {
            #[inline]
            fn prepare(&mut self, world: WorldId) {
                #![allow(non_snake_case)]
                let ($( $ty, )*) = &mut self.filters;
                $( $ty.prepare(world); )*
            }

            #[inline]
            fn matches_archetype<Fet: Fetch>(&mut self, fetch: &Fet) -> FilterResult {
                #![allow(non_snake_case)]
                let ($( $ty, )*) = &mut self.filters;
                let mut result = FilterResult::Defer;
                $( result = result.coalesce_and($ty.matches_archetype(fetch)); )*
                result
            }
        }

        impl<$( $ty ),*> std::ops::Not for And<($( $ty, )*)> {
            type Output = Not<Self>;

            #[inline]
            fn not(self) -> Self::Output {
                Not { filter: self }
            }
        }

        impl<$( $ty ),*, Rhs: ActiveFilter> std::ops::BitAnd<Rhs> for And<($( $ty, )*)> {
            type Output = And<($( $ty, )* Rhs)>;

            #[inline]
            fn bitand(self, rhs: Rhs) -> Self::Output {
                #![allow(non_snake_case)]
                let ($( $ty, )*) = self.filters;
                And {
                    filters: ($( $ty, )* rhs),
                }
            }
        }

        impl<$( $ty ),*> std::ops::BitAnd<Passthrough> for And<($( $ty, )*)> {
            type Output = Self;

            #[inline]
            fn bitand(self, _: Passthrough) -> Self::Output {
                self
            }
        }

        impl<$( $ty ),*, Rhs: ActiveFilter> std::ops::BitOr<Rhs> for And<($( $ty, )*)> {
            type Output = Or<(Self, Rhs)>;

            #[inline]
            fn bitor(self, rhs: Rhs) -> Self::Output {
                Or {
                    filters: (self, rhs),
                }
            }
        }

        impl<$( $ty ),*> std::ops::BitOr<Passthrough> for And<($( $ty, )*)> {
            type Output = Self;

            #[inline]
            fn bitor(self, _: Passthrough) -> Self::Output {
                self
            }
        }
    };
}

#[cfg(feature = "extended-tuple-impls")]
and_filter!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);

#[cfg(not(feature = "extended-tuple-impls"))]
and_filter!(A, B, C, D, E, F, G, H);