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
use crate::{
    component::{Component, ComponentId, ComponentList},
    entity::EntityId,
    world::UnsafeWorldCell,
};

pub trait QueryComponent {
    type Item<'world>: QueryComponent;

    fn get_component_ids() -> Vec<ComponentId>;

    fn get_component<'world>(
        world: UnsafeWorldCell<'world>,
        entity_id: EntityId,
    ) -> Self::Item<'world>;
}

macro_rules! impl_qc_for {
    ( $($n:ident),* ) => {
        #[allow(non_snake_case)]
        impl<$($n,)*> QueryComponent for ($($n,)*)
        where $($n: QueryComponent,)* {
            type Item<'world> = ($($n::Item<'world>,)*);

            fn get_component_ids() -> Vec<ComponentId> {
                let mut vec = Vec::new();

                $(vec.extend($n::get_component_ids());)*

                vec
            }

            fn get_component<'world>(
                world: UnsafeWorldCell<'world>,
                entity_id: EntityId,
            ) -> Self::Item<'world> {
                $(let $n = $n::get_component(world, entity_id);)*

                ($($n,)*)
            }
        }
    };
}

impl_qc_for!(T1);