flecs_ecs 0.2.2

Rust API for the C/CPP flecs ECS library <https://github.com/SanderMertens/flecs>
Documentation
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
use core::marker::PhantomData;

use crate::core::*;
use crate::sys;
use flecs_ecs_derive::tuples;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[doc(hidden)]
pub struct IsAnyArray {
    pub a_ref: bool, //e.g. singleton, prefab inheritance
    pub a_row: bool, //e.g. sparse, non_fragmenting
}

#[cfg(feature = "flecs_safety_locks")]
#[derive(Debug, Clone, Copy, Default)]
pub struct TableColumnSafety {
    //only set for sparse terms
    pub component_id: u64,
    pub table_record: *const sys::ecs_table_record_t,
}

pub struct ComponentsData<T: QueryTuple, const LEN: usize> {
    pub array_components: [*mut u8; LEN],
    pub is_ref_array_components: [bool; LEN],
    pub is_row_array_components: [bool; LEN],
    pub index_array_components: [i8; LEN],
    #[cfg(feature = "flecs_safety_locks")]
    pub safety_table_records: [TableColumnSafety; LEN],
    _marker: PhantomData<T>,
}

pub trait ComponentPointers<T: QueryTuple> {
    fn new(iter: &sys::ecs_iter_t) -> (IsAnyArray, Self);

    fn get_tuple(&mut self, index: usize) -> T::TupleType<'_>;

    fn get_tuple_with_row(
        &mut self,
        iter: &sys::ecs_iter_t,
        index_row_entity: usize,
    ) -> T::TupleType<'_>;

    fn get_tuple_with_ref(&mut self, index: usize) -> T::TupleType<'_>;

    #[cfg(feature = "flecs_safety_locks")]
    fn safety_table_records(&self) -> &[TableColumnSafety];
}

impl<T: QueryTuple, const LEN: usize> ComponentPointers<T> for ComponentsData<T, LEN> {
    fn new(iter: &sys::ecs_iter_t) -> (IsAnyArray, Self) {
        let mut array_components = [core::ptr::null::<u8>() as *mut u8; LEN];
        let mut is_ref_array_components = [false; LEN];
        let mut is_row_array_components = [false; LEN];
        let mut index_array_components = [0; LEN];
        #[cfg(feature = "flecs_safety_locks")]
        let mut safety_table_records = [TableColumnSafety::default(); LEN];

        let is_any_array = if (iter.ref_fields | iter.up_fields) != 0 {
            T::populate_array_ptrs(
                iter,
                &mut array_components[..],
                &mut is_ref_array_components[..],
                &mut is_row_array_components[..],
                &mut index_array_components[..],
                #[cfg(feature = "flecs_safety_locks")]
                &mut safety_table_records[..],
            )
        } else {
            // TODO since we know there is no is_ref and this always return false, we could mitigate a branch if we
            // split up the functions
            T::populate_self_array_ptrs(
                iter,
                &mut array_components[..],
                #[cfg(feature = "flecs_safety_locks")]
                &mut safety_table_records[..],
            );
            IsAnyArray {
                a_ref: false,
                a_row: false,
            }
        };

        (
            is_any_array,
            Self {
                array_components,
                is_ref_array_components,
                is_row_array_components,
                index_array_components,
                #[cfg(feature = "flecs_safety_locks")]
                safety_table_records,
                _marker: PhantomData::<T>,
            },
        )
    }

    #[inline(always)]
    fn get_tuple(&mut self, index: usize) -> T::TupleType<'_> {
        T::create_tuple(&self.array_components[..], index)
    }

    fn get_tuple_with_row(
        &mut self,
        iter: &sys::ecs_iter_t,
        index_row_entity: usize,
    ) -> T::TupleType<'_> {
        T::create_tuple_with_row(
            iter,
            &mut self.array_components[..],
            &self.is_ref_array_components[..],
            &self.is_row_array_components[..],
            &self.index_array_components[..],
            index_row_entity,
        )
    }

    fn get_tuple_with_ref(&mut self, index: usize) -> T::TupleType<'_> {
        T::create_tuple_with_ref(
            &self.array_components[..],
            &self.is_ref_array_components[..],
            index,
        )
    }

    #[cfg(feature = "flecs_safety_locks")]
    fn safety_table_records(&self) -> &[TableColumnSafety] {
        &self.safety_table_records[..]
    }
}

pub trait IterableTypeOperation {
    type CastType;
    type ActualType<'w>;
    type SliceType<'w>;
    type OnlyType: ComponentOrPairId;
    type OnlyPairType: ComponentId;
    const IS_IMMUTABLE: bool;
    const IS_OPTIONAL: bool;

    fn populate_term(term: &mut sys::ecs_term_t);

    fn create_tuple_data<'a>(array_components_data: *mut u8, index: usize) -> Self::ActualType<'a>;

    fn create_tuple_with_ref_data<'a>(
        array_components_data: *mut u8,
        is_ref: bool,
        index: usize,
    ) -> Self::ActualType<'a>;
}

impl<T> IterableTypeOperation for &T
where
    T: ComponentOrPairId,
{
    type CastType = *const <T as ComponentOrPairId>::CastType;
    type ActualType<'w> = &'w <T as ComponentOrPairId>::CastType;
    type SliceType<'w> = &'w [<T as ComponentOrPairId>::CastType];
    type OnlyType = T;
    type OnlyPairType = <T as ComponentOrPairId>::CastType;
    const IS_IMMUTABLE: bool = true;
    const IS_OPTIONAL: bool = false;

    #[inline(always)]
    fn populate_term(term: &mut sys::ecs_term_t) {
        term.inout = InOutKind::In as i16;
    }

    #[inline(always)]
    fn create_tuple_data<'a>(array_components_data: *mut u8, index: usize) -> Self::ActualType<'a> {
        let data_ptr = array_components_data as Self::CastType;
        unsafe { &*data_ptr.add(index) }
    }

    #[inline(always)]
    fn create_tuple_with_ref_data<'a>(
        array_components_data: *mut u8,
        is_ref: bool,
        index: usize,
    ) -> Self::ActualType<'a> {
        let data_ptr = array_components_data as Self::CastType;
        unsafe {
            if is_ref {
                &*data_ptr.add(0)
            } else {
                &*data_ptr.add(index)
            }
        }
    }
}

impl<T> IterableTypeOperation for &mut T
where
    T: ComponentOrPairId,
{
    type CastType = *mut <T as ComponentOrPairId>::CastType;
    type ActualType<'w> = &'w mut <T as ComponentOrPairId>::CastType;
    type SliceType<'w> = &'w mut [<T as ComponentOrPairId>::CastType];
    type OnlyType = T;
    type OnlyPairType = <T as ComponentOrPairId>::CastType;
    const IS_IMMUTABLE: bool = false;
    const IS_OPTIONAL: bool = false;

    #[inline(always)]
    fn populate_term(term: &mut sys::ecs_term_t) {
        term.inout = InOutKind::InOut as i16;
    }

    #[inline(always)]
    fn create_tuple_data<'a>(array_components_data: *mut u8, index: usize) -> Self::ActualType<'a> {
        let data_ptr = array_components_data as Self::CastType;
        unsafe { &mut *data_ptr.add(index) }
    }

    #[inline(always)]
    fn create_tuple_with_ref_data<'a>(
        array_components_data: *mut u8,
        is_ref: bool,
        index: usize,
    ) -> Self::ActualType<'a> {
        let data_ptr = array_components_data as Self::CastType;
        unsafe {
            if is_ref {
                &mut *data_ptr.add(0)
            } else {
                &mut *data_ptr.add(index)
            }
        }
    }
}

impl<T> IterableTypeOperation for Option<&T>
where
    T: ComponentOrPairId,
{
    type CastType = *const <T as ComponentOrPairId>::CastType;
    type ActualType<'w> = Option<&'w <T as ComponentOrPairId>::CastType>;
    type SliceType<'w> = Option<&'w [<T as ComponentOrPairId>::CastType]>;
    type OnlyType = T;
    type OnlyPairType = <T as ComponentOrPairId>::CastType;
    const IS_IMMUTABLE: bool = true;
    const IS_OPTIONAL: bool = true;

    fn populate_term(term: &mut sys::ecs_term_t) {
        term.inout = InOutKind::In as i16;
        term.oper = OperKind::Optional as i16;
    }

    #[inline(always)]
    fn create_tuple_data<'a>(array_components_data: *mut u8, index: usize) -> Self::ActualType<'a> {
        let data_ptr = array_components_data as Self::CastType;
        if data_ptr.is_null() {
            None
        } else {
            Some(unsafe { &*data_ptr.add(index) })
        }
    }

    #[inline(always)]
    fn create_tuple_with_ref_data<'a>(
        array_components_data: *mut u8,
        is_ref: bool,
        index: usize,
    ) -> Self::ActualType<'a> {
        let data_ptr = array_components_data as Self::CastType;
        if data_ptr.is_null() {
            None
        } else if is_ref {
            Some(unsafe { &*data_ptr.add(0) })
        } else {
            Some(unsafe { &*data_ptr.add(index) })
        }
    }
}

impl<T> IterableTypeOperation for Option<&mut T>
where
    T: ComponentOrPairId,
{
    type CastType = *mut <T as ComponentOrPairId>::CastType;
    type ActualType<'w> = Option<&'w mut <T as ComponentOrPairId>::CastType>;
    type SliceType<'w> = Option<&'w mut [<T as ComponentOrPairId>::CastType]>;
    type OnlyType = T;
    type OnlyPairType = <T as ComponentOrPairId>::CastType;
    const IS_IMMUTABLE: bool = false;
    const IS_OPTIONAL: bool = true;

    #[inline(always)]
    fn populate_term(term: &mut sys::ecs_term_t) {
        term.inout = InOutKind::InOut as i16;
        term.oper = OperKind::Optional as i16;
    }

    #[inline(always)]
    fn create_tuple_data<'a>(array_components_data: *mut u8, index: usize) -> Self::ActualType<'a> {
        let data_ptr = array_components_data as Self::CastType;
        if data_ptr.is_null() {
            None
        } else {
            Some(unsafe { &mut *data_ptr.add(index) })
        }
    }

    #[inline(always)]
    fn create_tuple_with_ref_data<'a>(
        array_components_data: *mut u8,
        is_ref: bool,
        index: usize,
    ) -> Self::ActualType<'a> {
        let data_ptr = array_components_data as Self::CastType;
        if data_ptr.is_null() {
            None
        } else if is_ref {
            Some(unsafe { &mut *data_ptr.add(0) })
        } else {
            Some(unsafe { &mut *data_ptr.add(index) })
        }
    }
}

pub trait QueryTuple: Sized {
    type Pointers: ComponentPointers<Self>;
    type TupleType<'a>;
    const CONTAINS_ANY_TAG_TERM: bool;
    const COUNT: i32;
    const COUNT_IMMUTABLE: usize;
    const COUNT_MUTABLE: usize;
    const COUNT_OPTIONAL_IMMUTABLE: usize;
    const COUNT_OPTIONAL_MUTABLE: usize;

    fn create_ptrs(iter: &sys::ecs_iter_t) -> (IsAnyArray, Self::Pointers) {
        Self::Pointers::new(iter)
    }

    fn populate<'a>(query: &mut impl QueryBuilderImpl<'a>);

    #[inline(always)]
    fn register_ids_descriptor(world: *mut sys::ecs_world_t, desc: &mut sys::ecs_query_desc_t) {
        Self::register_ids_descriptor_at(world, &mut desc.terms[..], &mut 0);
    }

    fn register_ids_descriptor_at(
        world: *mut sys::ecs_world_t,
        terms: &mut [sys::ecs_term_t],
        index: &mut usize,
    );

    fn populate_array_ptrs(
        it: &sys::ecs_iter_t,
        components: &mut [*mut u8],
        is_ref: &mut [bool],
        is_row: &mut [bool],
        indexes: &mut [i8],
        #[cfg(feature = "flecs_safety_locks")] table_records: &mut [TableColumnSafety],
    ) -> IsAnyArray;

    fn populate_self_array_ptrs(
        it: &sys::ecs_iter_t,
        components: &mut [*mut u8],
        #[cfg(feature = "flecs_safety_locks")] table_records: &mut [TableColumnSafety],
    );

    fn create_tuple(array_components: &[*mut u8], index: usize) -> Self::TupleType<'_>;

    fn create_tuple_with_ref<'a>(
        array_components: &'a [*mut u8],
        is_ref_array_components: &[bool],
        index: usize,
    ) -> Self::TupleType<'a>;

    fn create_tuple_with_row<'a>(
        iter: &sys::ecs_iter_t,
        array_components: &'a mut [*mut u8],
        is_ref_array_components: &[bool],
        is_row_array_components: &[bool],
        indexes_array_components: &[i8],
        index_row_entity: usize,
    ) -> Self::TupleType<'a>;
}

/////////////////////
// The higher sized tuples are done by a macro towards the bottom of this file.
/////////////////////

#[rustfmt::skip]
impl<A> QueryTuple for A
where
    A: IterableTypeOperation,
{
    type Pointers = ComponentsData<A, 1>;
    type TupleType<'w> = A::ActualType<'w>;
    const CONTAINS_ANY_TAG_TERM: bool = <<A::OnlyPairType as ComponentId>::UnderlyingType as ComponentInfo>::IS_TAG;
    const COUNT : i32 = 1;
    const COUNT_IMMUTABLE: usize = if A::IS_IMMUTABLE && !A::IS_OPTIONAL { 1 } else { 0 };
    const COUNT_MUTABLE: usize = if !A::IS_IMMUTABLE && !A::IS_OPTIONAL { 1 } else { 0 };
    const COUNT_OPTIONAL_IMMUTABLE: usize = if A::IS_IMMUTABLE && A::IS_OPTIONAL { 1 } else { 0 };
    const COUNT_OPTIONAL_MUTABLE: usize = if !A::IS_IMMUTABLE && A::IS_OPTIONAL { 1 } else { 0 };

    #[inline(always)]
    fn populate<'a>(query: &mut impl QueryBuilderImpl<'a>) {
        let _world_ptr = query.world_ptr();

        let id = <A::OnlyType as ComponentOrPairId>::get_id(query.world());

        if <A::OnlyType as ComponentOrPairId>::IS_PAIR {
            ecs_assert!(
                unsafe { sys::ecs_get_typeid(_world_ptr, id) } != 0,
                FlecsErrorCode::InvalidOperation,
                "Pair is not a (data) component. Possible cause: PairIsTag trait"
            );
        }
        
        query.with(id);
        let term = query.current_term_mut();
        A::populate_term(term);

    }

        #[expect(
        clippy::not_unsafe_ptr_arg_deref,
        reason = "x"
    )]
    #[inline(always)]
    fn register_ids_descriptor_at(
        world: *mut sys::ecs_world_t,
        terms: &mut [sys::ecs_term_t],
        index: &mut usize,
    ) {
        let world = unsafe { WorldRef::from_ptr(world) };
        terms[*index].id = <A::OnlyType as ComponentOrPairId>::get_id(world);
        A::populate_term(&mut terms[*index]);
        *index += 1;
    }

    #[inline(always)]
    fn populate_array_ptrs(
        it: &sys::ecs_iter_t,
        components: &mut [*mut u8],
        is_ref: &mut [bool],
        is_row: &mut [bool],
        indexes: &mut [i8],
        #[cfg(feature = "flecs_safety_locks")] table_records: &mut [TableColumnSafety],
    ) -> IsAnyArray {
        #[cfg(feature = "flecs_safety_locks")]
        let tr = unsafe { table_records.get_unchecked_mut(0) };
        #[cfg(feature = "flecs_safety_locks")]
        {
            tr.table_record = unsafe { *it.trs.add(0) };
        }

        #[cfg(not(feature = "flecs_term_count_64"))] 
        let val = 1u32 << 0;
        #[cfg(feature = "flecs_term_count_64")]
        let val = 1u64 << 0;
        if it.row_fields & val != 0 {
            // Need to fetch the value with flecs_field_at()
            is_ref[0] = true;
            is_row[0] = true;
            indexes[0] = 0;

            #[cfg(feature = "flecs_safety_locks")]
            {
                tr.component_id = unsafe { *it.ids.add(0) };
            }
        } else {
            components[0] = flecs_field::<A::OnlyPairType>(it, 0) as *mut u8 ;
            is_ref[0] = unsafe { *it.sources.add(0) != 0 };
        };

        IsAnyArray {
            a_ref: is_ref[0],
            a_row: is_row[0],
        }
    }

    #[inline(always)]
    fn populate_self_array_ptrs(
        it: &sys::ecs_iter_t,
        components: &mut [*mut u8],
        #[cfg(feature = "flecs_safety_locks")] table_records: &mut [TableColumnSafety],

    ) {
        #[cfg(feature = "flecs_safety_locks")]
        {
            let tr = unsafe { table_records.get_unchecked_mut(0) };
            tr.table_record = unsafe { *it.trs.add(0) };
        }
        components[0] = flecs_field::<A::OnlyPairType>(it, 0) as *mut u8 ;
    }

    #[inline(always)]
    fn create_tuple(array_components: &[*mut u8], index: usize) -> Self::TupleType<'_> {
        A::create_tuple_data(unsafe { *array_components.get_unchecked(0) }, index)

    }

    // TODO since it's only one component, we don't need to check if it's a ref array or not, we can just return the first element of the array
    // I think this is the case for all tuples of size 1
    fn create_tuple_with_ref<'a>(
        array_components: &'a [*mut u8],
        is_ref_array_components: &[bool],
        index: usize
    ) -> Self::TupleType<'a> {
        A::create_tuple_with_ref_data(array_components[0], is_ref_array_components[0], index)
    }

    #[inline(always)]
    fn create_tuple_with_row<'a>(
            iter: &sys::ecs_iter_t,
            array_components: &'a mut [*mut u8],
            is_ref_array_components: &[bool],
            is_row_array_components: &[bool],
            indexes_array_components: &[i8],
            index_row_entity: usize
        ) -> Self::TupleType<'a> {

        if is_row_array_components[0] {
            let ptr_to_first_index_array = &mut array_components[0];
            *ptr_to_first_index_array = unsafe { flecs_field_at::<A::OnlyPairType>(iter, *indexes_array_components.get_unchecked(0), index_row_entity as i32) } as *mut u8;
        }

        A::create_tuple_with_ref_data(
            array_components[0],
            is_ref_array_components[0],
            index_row_entity,
        )
    }
}

macro_rules! tuple_count {
    () => { 0 };
    ($head:ident) => { 1 };
    ($head:ident, $($tail:ident),*) => { 1 + tuple_count!($($tail),*) };
}

// macro_rules! count_immutable {
//     () => { 0 };
//     ($head:ident) => { if $head::IS_IMMUTABLE && !$head::IS_OPTIONAL { 1 } else { 0 } };
//     ($head:ident, $($tail:ident),*) => {
//         (if $head::IS_IMMUTABLE && !$head::IS_OPTIONAL { 1 } else { 0 }) + count_immutable!($($tail),*)
//     };
// }

// macro_rules! count_mutable {
//     () => { 0 };
//     ($head:ident) => { if !$head::IS_IMMUTABLE && !$head::IS_OPTIONAL { 1 } else { 0 } };
//     ($head:ident, $($tail:ident),*) => {
//         (if !$head::IS_IMMUTABLE && !$head::IS_OPTIONAL { 1 } else { 0 }) + count_mutable!($($tail),*)
//     };
// }

macro_rules! impl_iterable {
    ($($t:ident),*) => {
        impl<$($t: IterableTypeOperation),*> QueryTuple for ($($t,)*) {
            type TupleType<'w> = ($(
                $t::ActualType<'w>,
            )*);

            const CONTAINS_ANY_TAG_TERM: bool = $(<<$t::OnlyPairType as ComponentId>::UnderlyingType as ComponentInfo>::IS_TAG ||)* false;

            type Pointers = ComponentsData<Self, { tuple_count!($($t),*) }>;
            const COUNT : i32 = tuple_count!($($t),*);
            const COUNT_IMMUTABLE: usize = $((if $t::IS_IMMUTABLE && !$t::IS_OPTIONAL { 1 } else { 0 }) +)* 0;
            const COUNT_MUTABLE: usize = $((if !$t::IS_IMMUTABLE && !$t::IS_OPTIONAL { 1 } else { 0 }) +)* 0;
            const COUNT_OPTIONAL_IMMUTABLE: usize = $((if $t::IS_IMMUTABLE && $t::IS_OPTIONAL { 1 } else { 0 }) +)* 0;
            const COUNT_OPTIONAL_MUTABLE: usize = $((if !$t::IS_IMMUTABLE && $t::IS_OPTIONAL { 1 } else { 0 }) +)* 0;

            #[inline(always)]
            fn populate<'a>(query: &mut impl QueryBuilderImpl<'a>) {
                let _world = query.world();
                let _world_ptr = query.world_ptr();

                $(
                    let id = <$t::OnlyType as ComponentOrPairId>::get_id(_world);

                    if <$t::OnlyType as ComponentOrPairId>::IS_PAIR {
                        ecs_assert!(
                            unsafe { sys::ecs_get_typeid(_world_ptr, id) } != 0,
                            FlecsErrorCode::InvalidOperation,
                            "Pair is not a (data) component. Possible cause: PairIsTag trait"
                        );
                    }

                    query.with(id);
                    let term = query.current_term_mut();
                    $t::populate_term(term);

                )*
            }

            #[allow(unused)]
            #[inline(always)]
            fn register_ids_descriptor_at(world: *mut sys::ecs_world_t, terms: &mut [sys::ecs_term_t], index: &mut usize) {
                $( $t::register_ids_descriptor_at(world, terms, index); )*
            }

            #[allow(unused)]
            #[inline(always)]
            fn populate_array_ptrs(
                it: &sys::ecs_iter_t,
                components: &mut [*mut u8],
                is_ref: &mut [bool],
                is_row: &mut [bool],
                indexes: &mut [i8],
                #[cfg(feature = "flecs_safety_locks")] table_records: &mut [TableColumnSafety],
            ) -> IsAnyArray {
                let mut index : usize = 0;
                let mut any_ref = false;
                let mut any_row = false;
                #[cfg(feature = "flecs_safety_locks")]
                let mut index_immutable : usize = 0;
                #[cfg(feature = "flecs_safety_locks")]
                let mut index_mutable : usize = const { Self::COUNT_IMMUTABLE };
                #[cfg(feature = "flecs_safety_locks")]
                let mut index_optional_immutable : usize = const { Self::COUNT_IMMUTABLE + Self::COUNT_MUTABLE };
                #[cfg(feature = "flecs_safety_locks")]
                let mut index_optional_mutable : usize = const { Self::COUNT_IMMUTABLE + Self::COUNT_MUTABLE + Self::COUNT_OPTIONAL_IMMUTABLE };
                $(
                    #[cfg(feature = "flecs_safety_locks")]
                    let idx = match ($t::IS_IMMUTABLE, $t::IS_OPTIONAL) {
                            (true,  false) => &mut index_immutable,
                            (true,  true)  => &mut index_optional_immutable,
                            (false, false) => &mut index_mutable,
                            (false, true)  => &mut index_optional_mutable,
                        };

                    #[cfg(feature = "flecs_safety_locks")]
                    let tr = unsafe { table_records.get_unchecked_mut(*idx) };

                    #[cfg(feature = "flecs_safety_locks")]
                    {
                        tr.table_record = unsafe { *it.trs.add(index) };
                    }

                    #[cfg(feature = "flecs_safety_locks")]
                    {
                        *idx += 1;
                    }

                    #[cfg(not(feature = "flecs_term_count_64"))]
                    let val = 1u32 << index;
                    #[cfg(feature = "flecs_term_count_64")]
                    let val = 1u64 << index;
                    if (it.row_fields & val) != 0 {
                        // Need to fetch the value with flecs_field_at()
                        is_ref[index] =  true;
                        is_row[index] = true;
                        indexes[index] = index as i8;
                        any_ref |= true;
                        any_row |= true;
                        #[cfg(feature = "flecs_safety_locks")]
                        {
                            tr.component_id = unsafe { *it.ids.add(index) };
                        }
                    } else {
                        components[index] =
                            flecs_field::<$t::OnlyPairType>(it, index as i8) as *mut u8;
                        let is_ref_val = unsafe { *it.sources.add(index ) != 0 };
                        is_ref[index] = is_ref_val;
                        any_ref |= is_ref_val;
                    }
                    index += 1;
                )*
                IsAnyArray {
                    a_ref: any_ref,
                    a_row: any_row,
                }
            }

            #[allow(unused)]
            #[inline(always)]
            fn populate_self_array_ptrs(
                it: &sys::ecs_iter_t,
                components: &mut [*mut u8],
                #[cfg(feature = "flecs_safety_locks")] table_records: &mut [TableColumnSafety],
            ) {
                let mut index : usize = 0;
                #[cfg(feature = "flecs_safety_locks")]
                let mut index_immutable : usize = 0;
                #[cfg(feature = "flecs_safety_locks")]
                let mut index_mutable : usize = const { Self::COUNT_IMMUTABLE };
                #[cfg(feature = "flecs_safety_locks")]
                let mut index_optional_immutable : usize = const { Self::COUNT_IMMUTABLE + Self::COUNT_MUTABLE };
                #[cfg(feature = "flecs_safety_locks")]
                let mut index_optional_mutable : usize = const { Self::COUNT_IMMUTABLE + Self::COUNT_MUTABLE + Self::COUNT_OPTIONAL_IMMUTABLE };
                $(
                    components[index] =
                        flecs_field::<$t::OnlyPairType>(it, index as i8) as *mut u8;
                    #[cfg(feature = "flecs_safety_locks")]
                    {
                        let idx = match ($t::IS_IMMUTABLE, $t::IS_OPTIONAL) {
                            (true,  false) => &mut index_immutable,
                            (true,  true)  => &mut index_optional_immutable,
                            (false, false) => &mut index_mutable,
                            (false, true)  => &mut index_optional_mutable,
                        };

                        let tr = unsafe { table_records.get_unchecked_mut(*idx) };
                        tr.table_record = unsafe { *it.trs.add(index) };
                        *idx += 1;
                    }
                    index += 1;
                )*

            }

            #[allow(unused, clippy::unused_unit)]
            #[inline(always)]
            fn create_tuple(array_components: &[*mut u8], index: usize) -> Self::TupleType<'_> {
                let mut column: usize = 0;

                ($({
                    let data_ptr = unsafe { *array_components.get_unchecked(column) };
                    column += 1;
                    $t::create_tuple_data(data_ptr, index)
                },)*)
            }

            #[allow(unused, clippy::unused_unit)]
            #[inline(always)]
            fn create_tuple_with_ref<'a>(array_components: &'a [*mut u8], is_ref_array_components: &[bool], index: usize) -> Self::TupleType<'a> {
                let mut column: usize = 0;
                ($({
                    let data_ptr = unsafe { *array_components.get_unchecked(column) };
                    let is_ref = unsafe { *is_ref_array_components.get_unchecked(column) };
                    column += 1;
                    $t::create_tuple_with_ref_data(data_ptr, is_ref, index)
                },)*)
            }

            #[allow(unused, clippy::unused_unit)]
            #[inline(always)]
            fn create_tuple_with_row<'a>(
                iter: &sys::ecs_iter_t,
                array_components: &'a mut [*mut u8],
                is_ref_array_components: &[bool],
                is_row_array_components: &[bool],
                indexes_array_components: &[i8],
                index_row_entity: usize
            ) -> Self::TupleType<'a> {
                let mut column: usize = 0;
                ($({
                    let is_row = unsafe { *is_row_array_components.get_unchecked(column) };
                    if is_row {
                        let ptr_to_first_index_array = unsafe { &mut *array_components.get_unchecked_mut(column) };
                        let index_array_component = unsafe { *indexes_array_components.get_unchecked(column) };
                        *ptr_to_first_index_array = unsafe { flecs_field_at::<$t::OnlyPairType>(iter, index_array_component, index_row_entity as i32) } as *mut $t::OnlyPairType as *mut u8;
                    }
                    let data_ptr = unsafe { *array_components.get_unchecked(column) };
                    let is_ref = unsafe { *is_ref_array_components.get_unchecked(column) };
                    column += 1;
                    $t::create_tuple_with_ref_data(data_ptr, is_ref, index_row_entity)
                },)*)
            }
        }
    }
}

tuples!(impl_iterable, 0, 32);

// #[cfg(test)]
// mod count_tests {
//     use super::*;
//     use crate::prelude::*;

//     #[derive(Component)]
//     struct Position {
//         x: f32,
//         y: f32,
//     }

//     #[derive(Component)]
//     struct Velocity {
//         x: f32,
//         y: f32,
//     }

//     #[derive(Component)]
//     struct Health(u32);

//     #[test]
//     fn test_count_immutable() {
//         // Test single types
//         assert_eq!(<&Position as QueryTuple>::COUNT_IMMUTABLE, 1);
//         assert_eq!(<&mut Position as QueryTuple>::COUNT_IMMUTABLE, 0);
//         assert_eq!(<Option<&Position> as QueryTuple>::COUNT_IMMUTABLE, 0);
//         assert_eq!(<Option<&mut Position> as QueryTuple>::COUNT_IMMUTABLE, 0);

//         // Test tuple types
//         assert_eq!(<(&Position, &Velocity) as QueryTuple>::COUNT_IMMUTABLE, 2);
//         assert_eq!(
//             <(&Position, &mut Velocity) as QueryTuple>::COUNT_IMMUTABLE,
//             1
//         );
//         assert_eq!(
//             <(&mut Position, &mut Velocity) as QueryTuple>::COUNT_IMMUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&Position, Option<&Velocity>, &Health) as QueryTuple>::COUNT_IMMUTABLE,
//             2
//         );
//         assert_eq!(
//             <(&Position, Option<&mut Velocity>, &Health) as QueryTuple>::COUNT_IMMUTABLE,
//             2
//         );

//         // Test larger tuples
//         assert_eq!(
//             <(&Position, &Velocity, &Health) as QueryTuple>::COUNT_IMMUTABLE,
//             3
//         );
//         assert_eq!(
//             <(&mut Position, &mut Velocity, &mut Health) as QueryTuple>::COUNT_IMMUTABLE,
//             0
//         );
//         assert_eq!(<(Option<&Position>, Option<&Velocity>, Option<&Health>) as QueryTuple>::COUNT_IMMUTABLE, 0);
//         assert_eq!(
//             <(&Position, &mut Velocity, &Health, Option<&Position>) as QueryTuple>::COUNT_IMMUTABLE,
//             2
//         );
//     }

//     #[test]
//     fn test_count_mutable() {
//         // Test single types
//         assert_eq!(<&Position as QueryTuple>::COUNT_MUTABLE, 0);
//         assert_eq!(<&mut Position as QueryTuple>::COUNT_MUTABLE, 1);
//         assert_eq!(<Option<&Position> as QueryTuple>::COUNT_MUTABLE, 0);
//         assert_eq!(<Option<&mut Position> as QueryTuple>::COUNT_MUTABLE, 0);

//         // Test tuple types
//         assert_eq!(<(&Position, &Velocity) as QueryTuple>::COUNT_MUTABLE, 0);
//         assert_eq!(<(&Position, &mut Velocity) as QueryTuple>::COUNT_MUTABLE, 1);
//         assert_eq!(
//             <(&mut Position, &mut Velocity) as QueryTuple>::COUNT_MUTABLE,
//             2
//         );
//         assert_eq!(
//             <(&Position, Option<&Velocity>, &Health) as QueryTuple>::COUNT_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&Position, Option<&mut Velocity>, &Health) as QueryTuple>::COUNT_MUTABLE,
//             0
//         );

//         // Test larger tuples
//         assert_eq!(
//             <(&Position, &Velocity, &Health) as QueryTuple>::COUNT_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&mut Position, &mut Velocity, &mut Health) as QueryTuple>::COUNT_MUTABLE,
//             3
//         );
//         assert_eq!(
//             <(Option<&Position>, Option<&Velocity>, Option<&Health>) as QueryTuple>::COUNT_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&Position, &mut Velocity, &Health, Option<&Position>) as QueryTuple>::COUNT_MUTABLE,
//             1
//         );
//     }

//     #[test]
//     fn test_count_optional_immutable() {
//         // Test single types
//         assert_eq!(<&Position as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE, 0);
//         assert_eq!(<&mut Position as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE, 0);
//         assert_eq!(
//             <Option<&Position> as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             1
//         );
//         assert_eq!(
//             <Option<&mut Position> as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             0
//         );

//         // Test tuple types
//         assert_eq!(
//             <(&Position, &Velocity) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&Position, &mut Velocity) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&mut Position, &mut Velocity) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&Position, Option<&Velocity>, &Health) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             1
//         );
//         assert_eq!(
//             <(&Position, Option<&mut Velocity>, &Health) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             0
//         );

//         // Test larger tuples
//         assert_eq!(
//             <(&Position, &Velocity, &Health) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&mut Position, &mut Velocity, &mut Health) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             0
//         );
//         assert_eq!(<(Option<&Position>, Option<&Velocity>, Option<&Health>) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE, 3);
//         assert_eq!(
//             <(&Position, &mut Velocity, &Health, Option<&Position>) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             1
//         );
//         assert_eq!(
//             <(Option<&Position>, Option<&mut Velocity>, Option<&Health>) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE,
//             2
//         );
//     }

//     #[test]
//     fn test_count_optional_mutable() {
//         // Test single types
//         assert_eq!(<&Position as QueryTuple>::COUNT_OPTIONAL_MUTABLE, 0);
//         assert_eq!(<&mut Position as QueryTuple>::COUNT_OPTIONAL_MUTABLE, 0);
//         assert_eq!(<Option<&Position> as QueryTuple>::COUNT_OPTIONAL_MUTABLE, 0);
//         assert_eq!(
//             <Option<&mut Position> as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             1
//         );

//         // Test tuple types
//         assert_eq!(
//             <(&Position, &Velocity) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&Position, &mut Velocity) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&mut Position, &mut Velocity) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&Position, Option<&Velocity>, &Health) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&Position, Option<&mut Velocity>, &Health) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             1
//         );

//         // Test larger tuples
//         assert_eq!(
//             <(&Position, &Velocity, &Health) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(&mut Position, &mut Velocity, &mut Health) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             0
//         );
//         assert_eq!(<(Option<&Position>, Option<&Velocity>, Option<&Health>) as QueryTuple>::COUNT_OPTIONAL_MUTABLE, 0);
//         assert_eq!(
//             <(&Position, &mut Velocity, &Health, Option<&Position>) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             0
//         );
//         assert_eq!(
//             <(Option<&Position>, Option<&mut Velocity>, Option<&Health>) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             1
//         );
//         assert_eq!(
//             <(
//                 Option<&mut Position>,
//                 Option<&mut Velocity>,
//                 Option<&mut Health>
//             ) as QueryTuple>::COUNT_OPTIONAL_MUTABLE,
//             3
//         );
//     }

//     #[test]
//     fn test_total_counts_add_up() {
//         // Verify that all counts add up to the total count
//         // Using const assertions instead of type alias to avoid lifetime issues

//         const TOTAL_COUNT: usize = <(
//             &Position,
//             &mut Velocity,
//             Option<&Health>,
//             Option<&mut Position>,
//         ) as QueryTuple>::COUNT;
//         const IMMUTABLE_COUNT: usize = <(
//             &Position,
//             &mut Velocity,
//             Option<&Health>,
//             Option<&mut Position>,
//         ) as QueryTuple>::COUNT_IMMUTABLE;
//         const MUTABLE_COUNT: usize = <(
//             &Position,
//             &mut Velocity,
//             Option<&Health>,
//             Option<&mut Position>,
//         ) as QueryTuple>::COUNT_MUTABLE;
//         const OPTIONAL_IMMUTABLE_COUNT: usize = <(
//             &Position,
//             &mut Velocity,
//             Option<&Health>,
//             Option<&mut Position>,
//         ) as QueryTuple>::COUNT_OPTIONAL_IMMUTABLE;
//         const OPTIONAL_MUTABLE_COUNT: usize = <(
//             &Position,
//             &mut Velocity,
//             Option<&Health>,
//             Option<&mut Position>,
//         ) as QueryTuple>::COUNT_OPTIONAL_MUTABLE;

//         assert_eq!(TOTAL_COUNT, 4);
//         assert_eq!(IMMUTABLE_COUNT, 1);
//         assert_eq!(MUTABLE_COUNT, 1);
//         assert_eq!(OPTIONAL_IMMUTABLE_COUNT, 1);
//         assert_eq!(OPTIONAL_MUTABLE_COUNT, 1);

//         // Verify they all add up
//         assert_eq!(
//             TOTAL_COUNT,
//             IMMUTABLE_COUNT + MUTABLE_COUNT + OPTIONAL_IMMUTABLE_COUNT + OPTIONAL_MUTABLE_COUNT
//         );
//     }
// }