wecs_core/query/
query_component.rs1use crate::{
2 component::{Component, ComponentId, ComponentList},
3 entity::EntityId,
4 world::UnsafeWorldCell,
5};
6
7pub trait QueryComponent {
8 type Item<'world>: QueryComponent;
9
10 fn get_component_ids() -> Vec<ComponentId>;
11
12 fn get_component<'world>(
13 world: UnsafeWorldCell<'world>,
14 entity_id: EntityId,
15 ) -> Self::Item<'world>;
16}
17
18macro_rules! impl_qc_for {
19 ( $($n:ident),* ) => {
20 #[allow(non_snake_case)]
21 impl<$($n,)*> QueryComponent for ($($n,)*)
22 where $($n: QueryComponent,)* {
23 type Item<'world> = ($($n::Item<'world>,)*);
24
25 fn get_component_ids() -> Vec<ComponentId> {
26 let mut vec = Vec::new();
27
28 $(vec.extend($n::get_component_ids());)*
29
30 vec
31 }
32
33 fn get_component<'world>(
34 world: UnsafeWorldCell<'world>,
35 entity_id: EntityId,
36 ) -> Self::Item<'world> {
37 $(let $n = $n::get_component(world, entity_id);)*
38
39 ($($n,)*)
40 }
41 }
42 };
43}
44
45impl_qc_for!(T1);