Lisible.xyz

First article - 2022-11-22 (1 min. read)

Is this working?

Yeah it seems like it’s working.

Here is some funny code:

 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
pub trait Query<'a> {
    type ResultType: 'a;

    fn fetch(index: EntityIndex, components: &'a Components) -> Option<Self::ResultType>;
    fn matching_ids(entity_count: usize, components: &'a Components) -> HashSet<EntityIndex>;
    fn type_ids() -> Vec<ComponentTypeId>;
}

macro_rules! impl_query_tuples {
    ($th:tt, $($t:tt,)*) => {
        impl<'a, $th, $($t,)*> Query<'a> for ($th, $($t,)*)
        where
            $th: Accessor<'a>,
            $($t: Accessor<'a>,)*
        {
            type ResultType = (EntityIndex, ($th::RefType, $($t::RefType,)*));

            fn fetch(index: EntityIndex, components: &'a Components) -> Option<Self::ResultType> {
                Some((index, ($th::fetch(index, components)?, $($t::fetch(index, components)?,)*)))
            }

            #[allow(unused_mut)]
            fn matching_ids(entity_count: usize, components: &'a Components) -> HashSet<EntityIndex> {
                let mut result = $th::matching_ids(entity_count, components);
                $(result = result.intersection(&$t::matching_ids(entity_count, components)).cloned().collect();)*
                result
            }

            fn type_ids() -> Vec<ComponentTypeId> {
                vec![$th::type_id(), $($t::type_id(),)*]
            }
        }
    }
}