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
use std::marker::PhantomData;

use super::{
    and::And, not::Not, or::Or, passthrough::Passthrough, ActiveFilter, FilterResult, GroupMatcher,
    LayoutFilter,
};
use crate::internals::storage::component::{Component, ComponentTypeId};

/// A filter which matches `true` when the given component exists in the archetype.
#[derive(Debug)]
pub struct ComponentFilter<T: Component>(PhantomData<T>);

impl<T: Component> Default for ComponentFilter<T> {
    fn default() -> Self {
        ComponentFilter(PhantomData)
    }
}

impl<T: Component> Copy for ComponentFilter<T> {}
impl<T: Component> Clone for ComponentFilter<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T: Component> ActiveFilter for ComponentFilter<T> {}

impl<T: Component> GroupMatcher for ComponentFilter<T> {
    fn can_match_group() -> bool {
        true
    }
    fn group_components() -> Vec<ComponentTypeId> {
        vec![ComponentTypeId::of::<T>()]
    }
}

impl<T: Component> LayoutFilter for ComponentFilter<T> {
    fn matches_layout(&self, components: &[ComponentTypeId]) -> FilterResult {
        FilterResult::Match(components.contains(&ComponentTypeId::of::<T>()))
    }
}

impl<T: Component> std::ops::Not for ComponentFilter<T> {
    type Output = Not<Self>;

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

impl<'a, T: Component, Rhs: ActiveFilter> std::ops::BitAnd<Rhs> for ComponentFilter<T> {
    type Output = And<(Self, Rhs)>;

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

impl<'a, T: Component> std::ops::BitAnd<Passthrough> for ComponentFilter<T> {
    type Output = Self;

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

impl<'a, T: Component, Rhs: ActiveFilter> std::ops::BitOr<Rhs> for ComponentFilter<T> {
    type Output = Or<(Self, Rhs)>;

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

impl<'a, T: Component> std::ops::BitOr<Passthrough> for ComponentFilter<T> {
    type Output = Self;

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