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
use super::{
    and::And, not::Not, passthrough::Passthrough, ActiveFilter, DynamicFilter, FilterResult,
    GroupMatcher, LayoutFilter,
};
use crate::internals::{query::view::Fetch, storage::component::ComponentTypeId, world::WorldId};

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

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

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

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

        impl<$( $ty ),*> GroupMatcher for Or<($( $ty, )*)> {
            fn can_match_group() -> bool {
                false
            }
            fn group_components() -> Vec<ComponentTypeId> {
                vec![]
            }
        }

        impl<$( $ty: LayoutFilter ),*> LayoutFilter for Or<($( $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_or($ty.matches_layout(components)); )*
                result
            }
        }

        impl<$( $ty: DynamicFilter ),*> DynamicFilter for Or<($( $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_or($ty.matches_archetype(fetch)); )*
                result
            }
        }

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

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

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

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

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

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

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

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

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

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

#[cfg(feature = "extended-tuple-impls")]
or_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"))]
or_filter!(A, B, C, D, E, F, G, H);