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
use crate::data::Index;

// TODO ECS: use this to handle optional components properly.
// pub trait OptionalComponentSet<T> {
//     fn get(&self, handle: Index) -> Option<&T>;
// }

/// A set of optional elements of type `T`.
pub trait ComponentSetOption<T>: Sync {
    /// Get the element associated to the given `handle`, if there is one.
    fn get(&self, handle: Index) -> Option<&T>;
}

/// A set of elements of type `T`.
pub trait ComponentSet<T>: ComponentSetOption<T> {
    /// The estimated number of elements in this set.
    ///
    /// This value is typically used for preallocating some arrays for
    /// better performances.
    fn size_hint(&self) -> usize;
    // TODO ECS: remove this, its only needed by the query pipeline update
    //           which should only take the modified colliders into account.
    /// Iterate through all the elements on this set.
    fn for_each(&self, f: impl FnMut(Index, &T));
    /// Get the element associated to the given `handle`.
    fn index(&self, handle: Index) -> &T {
        self.get(handle).unwrap()
    }
}

/// A set of mutable elements of type `T`.
pub trait ComponentSetMut<T>: ComponentSet<T> {
    /// Applies the given closure to the element associated to the given `handle`.
    ///
    /// Return `None` if the element doesn't exist.
    fn map_mut_internal<Result>(
        &mut self,
        handle: crate::data::Index,
        f: impl FnOnce(&mut T) -> Result,
    ) -> Option<Result>;

    /// Set the value of this element.
    fn set_internal(&mut self, handle: crate::data::Index, val: T);
}

/// Helper trait to address multiple elements at once.
pub trait BundleSet<'a, T> {
    /// Access multiple elements from this set.
    fn index_bundle(&'a self, handle: Index) -> T;
}

impl<'a, T, A, B> BundleSet<'a, (&'a A, &'a B)> for T
where
    T: ComponentSet<A> + ComponentSet<B>,
{
    #[inline(always)]
    fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B) {
        (self.index(handle), self.index(handle))
    }
}

impl<'a, T, A, B, C> BundleSet<'a, (&'a A, &'a B, &'a C)> for T
where
    T: ComponentSet<A> + ComponentSet<B> + ComponentSet<C>,
{
    #[inline(always)]
    fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B, &'a C) {
        (self.index(handle), self.index(handle), self.index(handle))
    }
}

impl<'a, T, A, B, C, D> BundleSet<'a, (&'a A, &'a B, &'a C, &'a D)> for T
where
    T: ComponentSet<A> + ComponentSet<B> + ComponentSet<C> + ComponentSet<D>,
{
    #[inline(always)]
    fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B, &'a C, &'a D) {
        (
            self.index(handle),
            self.index(handle),
            self.index(handle),
            self.index(handle),
        )
    }
}

impl<'a, T, A, B, C, D, E> BundleSet<'a, (&'a A, &'a B, &'a C, &'a D, &'a E)> for T
where
    T: ComponentSet<A> + ComponentSet<B> + ComponentSet<C> + ComponentSet<D> + ComponentSet<E>,
{
    #[inline(always)]
    fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B, &'a C, &'a D, &'a E) {
        (
            self.index(handle),
            self.index(handle),
            self.index(handle),
            self.index(handle),
            self.index(handle),
        )
    }
}

impl<'a, T, A, B, C, D, E, F> BundleSet<'a, (&'a A, &'a B, &'a C, &'a D, &'a E, &'a F)> for T
where
    T: ComponentSet<A>
        + ComponentSet<B>
        + ComponentSet<C>
        + ComponentSet<D>
        + ComponentSet<E>
        + ComponentSet<F>,
{
    #[inline(always)]
    fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B, &'a C, &'a D, &'a E, &'a F) {
        (
            self.index(handle),
            self.index(handle),
            self.index(handle),
            self.index(handle),
            self.index(handle),
            self.index(handle),
        )
    }
}