pragma_ecs 1.0.2

Entity Component System for pragma.core-based game engines and frameworks, in pure Rust
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
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
//! What a query yields ([`QueryData`]) and which entities it visits ([`QueryFilter`]).
//!
//! Borrowing the columns of one table without `unsafe` (DECISIONS #13): the table's columns are
//! walked once with `iter_mut` and offered, as [`ColumnRef`]s, to every element of the query. A
//! write takes the exclusive reference, a read turns it into a shared one that further reads
//! copy. Each element then downcasts its column into a slice, and the rows of a table are the
//! element slices iterated in lockstep.
//!
//! Row filters ([`Added`], [`Changed`]) do not take part in that pass: they fill a mask of the
//! passing rows from the tick columns before the data columns are borrowed (DECISIONS #28).

use std::any::{Any, TypeId, type_name};
use std::fmt;
use std::iter::Copied;
use std::marker::PhantomData;
use std::{mem, slice};

use pragma_core::graph::{self, TaskContext};

use crate::access::{Access, AccessKind};
use crate::change::{Mut, MutIter, MutSlice};
use crate::component::{ColumnData, Ticks};
use crate::lend::Columns;
use crate::{Component, EcsError, Entity};

mod sealed {
    /// Keeps [`QueryData`](super::QueryData) and [`QueryFilter`](super::QueryFilter) closed:
    /// their machinery is internal and may change (DECISIONS #12).
    pub trait Sealed {}
}

pub(crate) use sealed::Sealed;

/// A column of a table offered to the elements of a query: still exclusive, or already shared
/// by a reading element.
#[doc(hidden)]
#[derive(Debug)]
pub enum ColumnRef<'w> {
    /// Nobody has taken the column yet.
    Unique(&'w mut dyn Any),
    /// A reading element has taken it; other reads copy the reference.
    Shared(&'w dyn Any),
}

/// Takes `column` for reading, leaving a shared reference for other readers.
fn take_shared<'w, T: Component>(column: &mut Option<ColumnRef<'w>>) -> Option<&'w [T]> {
    let shared: &'w dyn Any = match column.take()? {
        ColumnRef::Unique(c) => c,
        ColumnRef::Shared(c) => c,
    };
    *column = Some(ColumnRef::Shared(shared));
    shared
        .downcast_ref::<ColumnData<T>>()
        .map(ColumnData::values)
}

/// Takes `column` for writing; `None` (the column left as is) if a reader already shares it.
fn take_unique<'w, T: Component>(
    column: &mut Option<ColumnRef<'w>>,
) -> Option<(&'w mut [T], &'w mut [u64])> {
    match column.take()? {
        ColumnRef::Unique(c) => c.downcast_mut::<ColumnData<T>>().map(ColumnData::parts_mut),
        shared @ ColumnRef::Shared(_) => {
            *column = Some(shared);
            None
        }
    }
}

/// What a [`Query`](crate::Query) yields for every matching entity.
///
/// Implemented for:
///
/// | `Q` | yields | access | matches tables |
/// |---|---|---|---|
/// | `&T` | `&T` | read `T` | with `T` |
/// | `&mut T` | [`Mut<T>`] | write `T` | with `T` |
/// | `Option<&T>` | `Option<&T>` | read `T` | all |
/// | [`Entity`] | the entity id | none | all |
/// | `(Q1, Q2, …)`, up to 8 | a tuple of the items | the union | all of them |
///
/// `&mut T` yields a [`Mut<T>`], which dereferences to `T` and records a change when it is
/// written (DECISIONS #28). The trait is sealed: its methods are internal (DECISIONS #12).
///
/// ```
/// use pragma_ecs::{Entity, Query, World};
///
/// struct Position(i32);
/// struct Name(&'static str);
///
/// let mut world = World::new();
/// let a = world.spawn()?;
/// world.insert(a, Position(1))?;
/// world.insert(a, Name("a"))?;
/// let b = world.spawn()?;
/// world.insert(b, Position(2))?;
///
/// let mut query = Query::<(Entity, &mut Position, Option<&Name>)>::new()?;
/// for (entity, mut position, name) in query.iter_mut(&mut world)? {
///     position.0 *= 10;
///     assert_eq!(name.is_some(), entity == a);
/// }
/// assert_eq!(world.get::<Position>(b).map(|p| p.0), Some(20));
/// # Ok::<(), pragma_ecs::EcsError>(())
/// ```
pub trait QueryData: Sealed {
    /// The value yielded per entity; `'w` is the borrow of the world.
    type Item<'w>;

    /// Columns offered so far for one table.
    #[doc(hidden)]
    type State<'w>: Default;

    /// The rows of one table (or of a chunk of it): one slice per element.
    #[doc(hidden)]
    type Slice<'w>: Send;

    /// Iterates the rows of a [`Slice`](Self::Slice).
    #[doc(hidden)]
    type Iter<'w>: Iterator<Item = Self::Item<'w>>;

    /// Adds the element's component access to `access`.
    #[doc(hidden)]
    fn declare(access: &mut Access) -> Result<(), EcsError>;

    /// Whether the element uses the component type `ty`.
    #[doc(hidden)]
    fn holds(ty: TypeId) -> bool;

    /// Whether a table with the component types `types` (sorted) has what the element needs.
    #[doc(hidden)]
    fn matches(types: &[TypeId]) -> bool;

    /// Offers the column of type `ty`; the element takes it if it needs it.
    #[doc(hidden)]
    fn offer<'w>(state: &mut Self::State<'w>, ty: TypeId, column: &mut Option<ColumnRef<'w>>);

    /// The rows of the table once every column was offered; `None` if a needed column is
    /// missing. A write is stamped with the change tick `tick`.
    #[doc(hidden)]
    fn finish<'w>(
        state: Self::State<'w>,
        entities: &'w [Entity],
        tick: u64,
    ) -> Option<Self::Slice<'w>>;

    /// Splits the rows at `mid` (clamped to the row count).
    #[doc(hidden)]
    fn split_at<'w>(slice: Self::Slice<'w>, mid: usize) -> (Self::Slice<'w>, Self::Slice<'w>);

    /// Iterates the rows.
    #[doc(hidden)]
    fn iter<'w>(slice: Self::Slice<'w>) -> Self::Iter<'w>;

    /// The borrows of the lent columns a system holds during a frame (DECISIONS #18).
    #[doc(hidden)]
    type Guard<'w>;

    /// Walks the lent columns table by table, in ascending table order.
    #[doc(hidden)]
    type Cursor<'a>;

    /// Borrows the element's lent columns from the frame graph.
    #[doc(hidden)]
    fn acquire<'w>(ctx: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError>;

    /// The lent change ticks of component type `ty` in table `table`, if the element holds them.
    #[doc(hidden)]
    fn lent_ticks<'a>(guard: &'a Self::Guard<'_>, ty: TypeId, table: usize) -> Option<Ticks<'a>>;

    /// Starts walking the borrowed columns.
    #[doc(hidden)]
    fn cursor<'a>(guard: &'a mut Self::Guard<'_>) -> Self::Cursor<'a>;

    /// The rows of table `id` (ids ascending between calls), whose row entities are `entities`;
    /// `None` if a needed column is missing. A write is stamped with `tick`.
    #[doc(hidden)]
    fn lent<'a>(
        cursor: &mut Self::Cursor<'a>,
        id: usize,
        entities: &'a [Entity],
        tick: u64,
    ) -> Option<Self::Slice<'a>>;
}

/// The slot of table `id` in lent columns, if it holds the table's rows.
fn lent_shared<T>(columns: &[ColumnData<T>], id: usize, rows: usize) -> Option<&[T]> {
    columns
        .get(id)
        .map(ColumnData::values)
        .filter(|column| column.len() == rows)
}

/// The lent ticks of `T` in table `table`, if `ty` is `T`.
fn lent_ticks_of<T: Component>(
    columns: &Columns<T>,
    ty: TypeId,
    table: usize,
) -> Option<Ticks<'_>> {
    if ty == TypeId::of::<T>() {
        columns.tables().get(table).map(ColumnData::ticks)
    } else {
        None
    }
}

/// Walks lent columns for writing: the slots are split off one table at a time.
#[doc(hidden)]
#[derive(Debug)]
pub struct MutCursor<'a, T> {
    rest: &'a mut [ColumnData<T>],
    /// The table id of `rest[0]`.
    offset: usize,
}

/// A [`QueryData`] that only reads, so it can run on a shared `&World`
/// ([`Query::iter`](crate::Query::iter), [`Query::par_for_each`](crate::Query::par_for_each)).
///
/// Implemented for `&T`, `Option<&T>`, [`Entity`] and tuples of them.
///
/// ```
/// use pragma_ecs::{Entity, Query, ReadOnlyQueryData, World};
///
/// fn count<Q: ReadOnlyQueryData>(world: &World) -> Result<usize, pragma_ecs::EcsError> {
///     Ok(Query::<Q>::new()?.iter(world)?.count())
/// }
///
/// let mut world = World::new();
/// let entity = world.spawn()?;
/// world.insert(entity, 1_u32)?;
/// assert_eq!(count::<(Entity, &u32)>(&world)?, 1);
/// # Ok::<(), pragma_ecs::EcsError>(())
/// ```
pub trait ReadOnlyQueryData: QueryData {}

/// Restricts the entities a [`Query`](crate::Query) visits.
///
/// Implemented for `()` (no restriction), [`With<T>`] and [`Without<T>`] (whole tables, without
/// reading data), [`Added<T>`] and [`Changed<T>`] (rows, by their change ticks, DECISIONS #28),
/// and tuples of filters (up to 8; all must hold). The trait is sealed (DECISIONS #12).
///
/// ```
/// use pragma_ecs::{Entity, Query, With, Without, World};
///
/// struct Enemy;
/// struct Dead;
///
/// let mut world = World::new();
/// let alive = world.spawn()?;
/// world.insert(alive, Enemy)?;
/// let dead = world.spawn()?;
/// world.insert(dead, Enemy)?;
/// world.insert(dead, Dead)?;
///
/// let mut living = Query::<Entity, (With<Enemy>, Without<Dead>)>::new()?;
/// assert_eq!(living.iter(&world)?.collect::<Vec<_>>(), [alive]);
/// # Ok::<(), pragma_ecs::EcsError>(())
/// ```
pub trait QueryFilter: Sealed {
    /// Whether the filter keeps every row of a matching table (no row filter inside).
    #[doc(hidden)]
    const DENSE: bool;

    /// Whether a table with the component types `types` (sorted) passes the filter.
    #[doc(hidden)]
    fn matches(types: &[TypeId]) -> bool;

    /// Adds the filter's reads to `access`; a type the query's data already uses is merged.
    #[doc(hidden)]
    fn declare(access: &mut Access) -> Result<(), EcsError>;

    /// Clears the entries of `mask` (one per row of a matching table) whose row fails the
    /// filter; `ticks` looks up the change ticks of the table's column of a type.
    #[doc(hidden)]
    fn mask<'a>(ticks: &dyn Fn(TypeId) -> Option<Ticks<'a>>, last_run: u64, mask: &mut [bool]);

    /// The borrows of lent tick columns a system holds during a frame.
    #[doc(hidden)]
    type Guard<'w>;

    /// Borrows the lent columns the filter reads that the data `Q` does not already hold.
    #[doc(hidden)]
    fn acquire<'w, Q: QueryData>(ctx: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError>;

    /// The lent change ticks of component type `ty` in table `table`, if the filter holds them.
    #[doc(hidden)]
    fn lent_ticks<'a>(guard: &'a Self::Guard<'_>, ty: TypeId, table: usize) -> Option<Ticks<'a>>;
}

fn has<T: Component>(types: &[TypeId]) -> bool {
    types.binary_search(&TypeId::of::<T>()).is_ok()
}

impl<T: Component> Sealed for &T {}

impl<T: Component> QueryData for &T {
    type Item<'w> = &'w T;
    type State<'w> = Option<&'w [T]>;
    type Slice<'w> = &'w [T];
    type Iter<'w> = slice::Iter<'w, T>;

    fn declare(access: &mut Access) -> Result<(), EcsError> {
        access.add_component::<T>(AccessKind::Read)
    }

    fn holds(ty: TypeId) -> bool {
        ty == TypeId::of::<T>()
    }

    fn matches(types: &[TypeId]) -> bool {
        has::<T>(types)
    }

    fn offer<'w>(state: &mut Self::State<'w>, ty: TypeId, column: &mut Option<ColumnRef<'w>>) {
        if ty == TypeId::of::<T>() && state.is_none() {
            *state = take_shared(column);
        }
    }

    fn finish<'w>(
        state: Self::State<'w>,
        entities: &'w [Entity],
        _: u64,
    ) -> Option<Self::Slice<'w>> {
        debug_assert!(state.is_none_or(|s| s.len() == entities.len()));
        state
    }

    fn split_at<'w>(slice: Self::Slice<'w>, mid: usize) -> (Self::Slice<'w>, Self::Slice<'w>) {
        slice.split_at(mid.min(slice.len()))
    }

    fn iter<'w>(slice: Self::Slice<'w>) -> Self::Iter<'w> {
        slice.iter()
    }

    type Guard<'w> = graph::Res<'w, Columns<T>>;
    type Cursor<'a> = &'a [ColumnData<T>];

    fn acquire<'w>(ctx: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError> {
        ctx.read::<Columns<T>>().map_err(EcsError::graph)
    }

    fn lent_ticks<'a>(guard: &'a Self::Guard<'_>, ty: TypeId, table: usize) -> Option<Ticks<'a>> {
        lent_ticks_of(guard, ty, table)
    }

    fn cursor<'a>(guard: &'a mut Self::Guard<'_>) -> Self::Cursor<'a> {
        guard.tables()
    }

    fn lent<'a>(
        cursor: &mut Self::Cursor<'a>,
        id: usize,
        entities: &'a [Entity],
        _: u64,
    ) -> Option<Self::Slice<'a>> {
        lent_shared(cursor, id, entities.len())
    }
}

impl<T: Component> ReadOnlyQueryData for &T {}

impl<T: Component> Sealed for &mut T {}

impl<T: Component> QueryData for &mut T {
    type Item<'w> = Mut<'w, T>;
    type State<'w> = Option<(&'w mut [T], &'w mut [u64])>;
    type Slice<'w> = MutSlice<'w, T>;
    type Iter<'w> = MutIter<'w, T>;

    fn declare(access: &mut Access) -> Result<(), EcsError> {
        access.add_component::<T>(AccessKind::Write)
    }

    fn holds(ty: TypeId) -> bool {
        ty == TypeId::of::<T>()
    }

    fn matches(types: &[TypeId]) -> bool {
        has::<T>(types)
    }

    fn offer<'w>(state: &mut Self::State<'w>, ty: TypeId, column: &mut Option<ColumnRef<'w>>) {
        if ty == TypeId::of::<T>() && state.is_none() {
            *state = take_unique(column);
        }
    }

    fn finish<'w>(
        state: Self::State<'w>,
        entities: &'w [Entity],
        tick: u64,
    ) -> Option<Self::Slice<'w>> {
        let (values, ticks) = state?;
        debug_assert_eq!(values.len(), entities.len());
        MutSlice::new(values, ticks, tick)
    }

    fn split_at<'w>(slice: Self::Slice<'w>, mid: usize) -> (Self::Slice<'w>, Self::Slice<'w>) {
        slice.split_at(mid)
    }

    fn iter<'w>(slice: Self::Slice<'w>) -> Self::Iter<'w> {
        slice.into_iter()
    }

    type Guard<'w> = graph::ResMut<'w, Columns<T>>;
    type Cursor<'a> = MutCursor<'a, T>;

    fn acquire<'w>(ctx: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError> {
        ctx.write::<Columns<T>>().map_err(EcsError::graph)
    }

    fn lent_ticks<'a>(guard: &'a Self::Guard<'_>, ty: TypeId, table: usize) -> Option<Ticks<'a>> {
        lent_ticks_of(guard, ty, table)
    }

    fn cursor<'a>(guard: &'a mut Self::Guard<'_>) -> Self::Cursor<'a> {
        MutCursor {
            rest: guard.tables_mut(),
            offset: 0,
        }
    }

    fn lent<'a>(
        cursor: &mut Self::Cursor<'a>,
        id: usize,
        entities: &'a [Entity],
        tick: u64,
    ) -> Option<Self::Slice<'a>> {
        let rest = mem::take(&mut cursor.rest);
        let skip = id.checked_sub(cursor.offset)?.min(rest.len());
        let (column, rest) = rest.split_at_mut(skip).1.split_first_mut()?;
        cursor.rest = rest;
        cursor.offset = id.saturating_add(1);
        if column.len() != entities.len() {
            return None;
        }
        let (values, ticks) = column.parts_mut();
        MutSlice::new(values, ticks, tick)
    }
}

/// The rows of an `Option<&T>` element: the column, if the table has one, and the row count.
#[doc(hidden)]
#[derive(Debug)]
pub struct OptionSlice<'w, T> {
    values: Option<&'w [T]>,
    len: usize,
}

/// Iterates an [`OptionSlice`].
#[doc(hidden)]
#[derive(Debug)]
pub struct OptionIter<'w, T> {
    values: Option<slice::Iter<'w, T>>,
    remaining: usize,
}

impl<'w, T> Iterator for OptionIter<'w, T> {
    type Item = Option<&'w T>;

    fn next(&mut self) -> Option<Self::Item> {
        self.remaining = self.remaining.checked_sub(1)?;
        Some(self.values.as_mut().and_then(Iterator::next))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.remaining, Some(self.remaining))
    }
}

impl<T: Component> Sealed for Option<&T> {}

impl<T: Component> QueryData for Option<&T> {
    type Item<'w> = Option<&'w T>;
    type State<'w> = Option<&'w [T]>;
    type Slice<'w> = OptionSlice<'w, T>;
    type Iter<'w> = OptionIter<'w, T>;

    fn declare(access: &mut Access) -> Result<(), EcsError> {
        access.add_component::<T>(AccessKind::Read)
    }

    fn holds(ty: TypeId) -> bool {
        ty == TypeId::of::<T>()
    }

    fn matches(_: &[TypeId]) -> bool {
        true
    }

    fn offer<'w>(state: &mut Self::State<'w>, ty: TypeId, column: &mut Option<ColumnRef<'w>>) {
        <&T>::offer(state, ty, column);
    }

    fn finish<'w>(
        state: Self::State<'w>,
        entities: &'w [Entity],
        _: u64,
    ) -> Option<Self::Slice<'w>> {
        debug_assert!(state.is_none_or(|s| s.len() == entities.len()));
        Some(OptionSlice {
            values: state,
            len: entities.len(),
        })
    }

    fn split_at<'w>(slice: Self::Slice<'w>, mid: usize) -> (Self::Slice<'w>, Self::Slice<'w>) {
        let mid = mid.min(slice.len);
        let rest = slice.len.saturating_sub(mid);
        let (left, right) = match slice.values {
            Some(values) => {
                let (left, right) = values.split_at(mid.min(values.len()));
                (Some(left), Some(right))
            }
            None => (None, None),
        };
        (
            OptionSlice {
                values: left,
                len: mid,
            },
            OptionSlice {
                values: right,
                len: rest,
            },
        )
    }

    fn iter<'w>(slice: Self::Slice<'w>) -> Self::Iter<'w> {
        OptionIter {
            values: slice.values.map(<[T]>::iter),
            remaining: slice.len,
        }
    }

    type Guard<'w> = graph::Res<'w, Columns<T>>;
    type Cursor<'a> = &'a [ColumnData<T>];

    fn acquire<'w>(ctx: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError> {
        <&T>::acquire(ctx)
    }

    fn lent_ticks<'a>(guard: &'a Self::Guard<'_>, ty: TypeId, table: usize) -> Option<Ticks<'a>> {
        lent_ticks_of(guard, ty, table)
    }

    fn cursor<'a>(guard: &'a mut Self::Guard<'_>) -> Self::Cursor<'a> {
        guard.tables()
    }

    fn lent<'a>(
        cursor: &mut Self::Cursor<'a>,
        id: usize,
        entities: &'a [Entity],
        _: u64,
    ) -> Option<Self::Slice<'a>> {
        // A table without `T` has an empty slot; with rows, its length tells them apart.
        Some(OptionSlice {
            values: lent_shared(cursor, id, entities.len()),
            len: entities.len(),
        })
    }
}

impl<T: Component> ReadOnlyQueryData for Option<&T> {}

impl Sealed for Entity {}

impl QueryData for Entity {
    type Item<'w> = Entity;
    type State<'w> = ();
    type Slice<'w> = &'w [Entity];
    type Iter<'w> = Copied<slice::Iter<'w, Entity>>;

    fn declare(_: &mut Access) -> Result<(), EcsError> {
        Ok(())
    }

    fn holds(_: TypeId) -> bool {
        false
    }

    fn matches(_: &[TypeId]) -> bool {
        true
    }

    fn offer<'w>(_: &mut Self::State<'w>, _: TypeId, _: &mut Option<ColumnRef<'w>>) {}

    fn finish<'w>((): Self::State<'w>, entities: &'w [Entity], _: u64) -> Option<Self::Slice<'w>> {
        Some(entities)
    }

    fn split_at<'w>(slice: Self::Slice<'w>, mid: usize) -> (Self::Slice<'w>, Self::Slice<'w>) {
        slice.split_at(mid.min(slice.len()))
    }

    fn iter<'w>(slice: Self::Slice<'w>) -> Self::Iter<'w> {
        slice.iter().copied()
    }

    type Guard<'w> = ();
    type Cursor<'a> = ();

    fn acquire<'w>(_: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError> {
        Ok(())
    }

    fn lent_ticks<'a>((): &'a Self::Guard<'_>, _: TypeId, _: usize) -> Option<Ticks<'a>> {
        None
    }

    fn cursor<'a>((): &'a mut Self::Guard<'_>) -> Self::Cursor<'a> {}

    fn lent<'a>(
        (): &mut Self::Cursor<'a>,
        _: usize,
        entities: &'a [Entity],
        _: u64,
    ) -> Option<Self::Slice<'a>> {
        Some(entities)
    }
}

impl ReadOnlyQueryData for Entity {}

/// Iterates the element iterators of a tuple query in lockstep.
#[doc(hidden)]
#[derive(Debug)]
pub struct TupleIter<I>(I);

macro_rules! tuple_query {
    ($($name:ident $index:tt),+) => {
        // Both data and filter tuples.
        impl<$($name),+> Sealed for ($($name,)+) {}

        impl<$($name: QueryData),+> QueryData for ($($name,)+) {
            type Item<'w> = ($($name::Item<'w>,)+);
            type State<'w> = ($($name::State<'w>,)+);
            type Slice<'w> = ($($name::Slice<'w>,)+);
            type Iter<'w> = TupleIter<($($name::Iter<'w>,)+)>;

            fn declare(access: &mut Access) -> Result<(), EcsError> {
                $($name::declare(access)?;)+
                Ok(())
            }

            fn holds(ty: TypeId) -> bool {
                $($name::holds(ty))||+
            }

            fn matches(types: &[TypeId]) -> bool {
                $($name::matches(types))&&+
            }

            fn offer<'w>(
                state: &mut Self::State<'w>,
                ty: TypeId,
                column: &mut Option<ColumnRef<'w>>,
            ) {
                $($name::offer(&mut state.$index, ty, column);)+
            }

            fn finish<'w>(
                state: Self::State<'w>,
                entities: &'w [Entity],
                tick: u64,
            ) -> Option<Self::Slice<'w>> {
                Some(($($name::finish(state.$index, entities, tick)?,)+))
            }

            fn split_at<'w>(
                slice: Self::Slice<'w>,
                mid: usize,
            ) -> (Self::Slice<'w>, Self::Slice<'w>) {
                let halves = ($($name::split_at(slice.$index, mid),)+);
                (($(halves.$index.0,)+), ($(halves.$index.1,)+))
            }

            fn iter<'w>(slice: Self::Slice<'w>) -> Self::Iter<'w> {
                TupleIter(($($name::iter(slice.$index),)+))
            }

            type Guard<'w> = ($($name::Guard<'w>,)+);
            type Cursor<'a> = ($($name::Cursor<'a>,)+);

            fn acquire<'w>(ctx: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError> {
                Ok(($($name::acquire(ctx)?,)+))
            }

            fn lent_ticks<'a>(
                guard: &'a Self::Guard<'_>,
                ty: TypeId,
                table: usize,
            ) -> Option<Ticks<'a>> {
                None$(.or_else(|| $name::lent_ticks(&guard.$index, ty, table)))+
            }

            fn cursor<'a>(guard: &'a mut Self::Guard<'_>) -> Self::Cursor<'a> {
                ($($name::cursor(&mut guard.$index),)+)
            }

            fn lent<'a>(
                cursor: &mut Self::Cursor<'a>,
                id: usize,
                entities: &'a [Entity],
                tick: u64,
            ) -> Option<Self::Slice<'a>> {
                Some(($($name::lent(&mut cursor.$index, id, entities, tick)?,)+))
            }
        }

        impl<$($name: ReadOnlyQueryData),+> ReadOnlyQueryData for ($($name,)+) {}

        impl<$($name: Iterator),+> Iterator for TupleIter<($($name,)+)> {
            type Item = ($($name::Item,)+);

            #[inline(always)]
            fn next(&mut self) -> Option<Self::Item> {
                Some(($(self.0.$index.next()?,)+))
            }
        }

        impl<$($name: QueryFilter),+> QueryFilter for ($($name,)+) {
            const DENSE: bool = $($name::DENSE)&&+;

            fn matches(types: &[TypeId]) -> bool {
                $($name::matches(types))&&+
            }

            fn declare(access: &mut Access) -> Result<(), EcsError> {
                $($name::declare(access)?;)+
                Ok(())
            }

            fn mask<'a>(
                ticks: &dyn Fn(TypeId) -> Option<Ticks<'a>>,
                last_run: u64,
                mask: &mut [bool],
            ) {
                $($name::mask(ticks, last_run, mask);)+
            }

            type Guard<'w> = ($($name::Guard<'w>,)+);

            fn acquire<'w, Q: QueryData>(
                ctx: &TaskContext<'w>,
            ) -> Result<Self::Guard<'w>, EcsError> {
                Ok(($($name::acquire::<Q>(ctx)?,)+))
            }

            fn lent_ticks<'a>(
                guard: &'a Self::Guard<'_>,
                ty: TypeId,
                table: usize,
            ) -> Option<Ticks<'a>> {
                None$(.or_else(|| $name::lent_ticks(&guard.$index, ty, table)))+
            }
        }
    };
}

tuple_query!(A 0);
tuple_query!(A 0, B 1);
tuple_query!(A 0, B 1, C 2);
tuple_query!(A 0, B 1, C 2, D 3);
tuple_query!(A 0, B 1, C 2, D 3, E 4);
tuple_query!(A 0, B 1, C 2, D 3, E 4, F 5);
tuple_query!(A 0, B 1, C 2, D 3, E 4, F 5, G 6);
tuple_query!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);

impl Sealed for () {}

/// The filter methods of filters that look at whole tables only.
macro_rules! table_filter {
    () => {
        const DENSE: bool = true;

        fn declare(_: &mut Access) -> Result<(), EcsError> {
            Ok(())
        }

        fn mask<'a>(_: &dyn Fn(TypeId) -> Option<Ticks<'a>>, _: u64, _: &mut [bool]) {}

        type Guard<'w> = ();

        fn acquire<'w, Q: QueryData>(_: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError> {
            Ok(())
        }

        fn lent_ticks<'a>((): &'a Self::Guard<'_>, _: TypeId, _: usize) -> Option<Ticks<'a>> {
            None
        }
    };
}

impl QueryFilter for () {
    table_filter!();

    fn matches(_: &[TypeId]) -> bool {
        true
    }
}

/// A [`QueryFilter`]: only entities that have a `T` component, whose value is not read.
///
/// ```
/// use pragma_ecs::{Query, With, World};
///
/// struct Health(u32);
/// struct Player;
///
/// let mut world = World::new();
/// let player = world.spawn()?;
/// world.insert(player, Health(10))?;
/// world.insert(player, Player)?;
/// let monster = world.spawn()?;
/// world.insert(monster, Health(50))?;
///
/// let mut players = Query::<&Health, With<Player>>::new()?;
/// let total: u32 = players.iter(&world)?.map(|h| h.0).sum();
/// assert_eq!(total, 10);
/// # Ok::<(), pragma_ecs::EcsError>(())
/// ```
pub struct With<T: Component>(PhantomData<fn() -> T>);

impl<T: Component> fmt::Debug for With<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "With<{}>", type_name::<T>())
    }
}

impl<T: Component> Sealed for With<T> {}

impl<T: Component> QueryFilter for With<T> {
    table_filter!();

    fn matches(types: &[TypeId]) -> bool {
        has::<T>(types)
    }
}

/// A [`QueryFilter`]: only entities that do not have a `T` component.
///
/// ```
/// use pragma_ecs::{Query, Without, World};
///
/// struct Health(u32);
/// struct Dead;
///
/// let mut world = World::new();
/// let alive = world.spawn()?;
/// world.insert(alive, Health(10))?;
/// let dead = world.spawn()?;
/// world.insert(dead, Health(0))?;
/// world.insert(dead, Dead)?;
///
/// let mut living = Query::<&mut Health, Without<Dead>>::new()?;
/// for mut health in living.iter_mut(&mut world)? {
///     health.0 += 1;
/// }
/// assert_eq!(world.get::<Health>(alive).map(|h| h.0), Some(11));
/// assert_eq!(world.get::<Health>(dead).map(|h| h.0), Some(0));
/// # Ok::<(), pragma_ecs::EcsError>(())
/// ```
pub struct Without<T: Component>(PhantomData<fn() -> T>);

impl<T: Component> fmt::Debug for Without<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Without<{}>", type_name::<T>())
    }
}

impl<T: Component> Sealed for Without<T> {}

impl<T: Component> QueryFilter for Without<T> {
    table_filter!();

    fn matches(types: &[TypeId]) -> bool {
        !has::<T>(types)
    }
}

/// The filter methods of [`Added`] and [`Changed`]: a row passes if its `$which` tick is newer
/// than the tick of the query's last run.
macro_rules! tick_filter {
    ($which:ident) => {
        const DENSE: bool = false;

        fn matches(types: &[TypeId]) -> bool {
            has::<T>(types)
        }

        fn declare(access: &mut Access) -> Result<(), EcsError> {
            access.add_filter_read::<T>()
        }

        fn mask<'a>(ticks: &dyn Fn(TypeId) -> Option<Ticks<'a>>, last_run: u64, mask: &mut [bool]) {
            match ticks(TypeId::of::<T>()) {
                Some(ticks) if ticks.$which.len() == mask.len() => {
                    for (keep, tick) in mask.iter_mut().zip(ticks.$which) {
                        // `&=`, not `&&`: no branch, so the loop vectorizes.
                        *keep &= *tick > last_run;
                    }
                }
                // Unreachable: a matching table has the column.
                _ => mask.fill(false),
            }
        }

        type Guard<'w> = Option<graph::Res<'w, Columns<T>>>;

        fn acquire<'w, Q: QueryData>(ctx: &TaskContext<'w>) -> Result<Self::Guard<'w>, EcsError> {
            if Q::holds(TypeId::of::<T>()) {
                // The data's own guard has the ticks; a second borrow would conflict with it.
                Ok(None)
            } else {
                ctx.read::<Columns<T>>().map(Some).map_err(EcsError::graph)
            }
        }

        fn lent_ticks<'a>(
            guard: &'a Self::Guard<'_>,
            ty: TypeId,
            table: usize,
        ) -> Option<Ticks<'a>> {
            lent_ticks_of(guard.as_ref()?, ty, table)
        }
    };
}

/// A [`QueryFilter`]: only entities whose `T` component was added since the query (or the
/// system) last ran (DECISIONS #27, #28). Reads the change ticks of `T`.
///
/// A component counts as added when it is inserted into an entity that did not have one,
/// directly or by a command; replacing it does not count. On its first run a query sees every
/// existing `T` as added.
///
/// ```
/// use pragma_ecs::{Added, Entity, Query, World};
///
/// struct Player;
///
/// let mut world = World::new();
/// let first = world.spawn()?;
/// world.insert(first, Player)?;
///
/// let mut joined = Query::<Entity, Added<Player>>::new()?;
/// assert_eq!(joined.iter(&world)?.collect::<Vec<_>>(), [first]);
/// assert_eq!(joined.iter(&world)?.count(), 0); // nothing new since
///
/// let second = world.spawn()?;
/// world.insert(second, Player)?;
/// world.insert(first, Player)?; // a replacement, not an addition
/// assert_eq!(joined.iter(&world)?.collect::<Vec<_>>(), [second]);
/// # Ok::<(), pragma_ecs::EcsError>(())
/// ```
pub struct Added<T: Component>(PhantomData<fn() -> T>);

impl<T: Component> fmt::Debug for Added<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Added<{}>", type_name::<T>())
    }
}

impl<T: Component> Sealed for Added<T> {}

impl<T: Component> QueryFilter for Added<T> {
    tick_filter!(added);
}

/// A [`QueryFilter`]: only entities whose `T` component was added or changed since the query
/// (or the system) last ran (DECISIONS #27, #28). Reads the change ticks of `T`.
///
/// A component counts as changed when it is written through a [`Mut<T>`], borrowed with
/// [`World::get_mut`](crate::World::get_mut), replaced with
/// [`World::insert`](crate::World::insert), or added. A query does not see its own changes on
/// its next run. `Query<&mut T, Changed<T>>` is allowed: the filter is evaluated before the rows
/// are borrowed.
///
/// ```
/// use pragma_ecs::{Changed, Entity, Query, World};
///
/// struct Health(u32);
///
/// let mut world = World::new();
/// let a = world.spawn()?;
/// world.insert(a, Health(10))?;
/// let b = world.spawn()?;
/// world.insert(b, Health(10))?;
///
/// let mut hurt = Query::<Entity, Changed<Health>>::new()?;
/// assert_eq!(hurt.iter(&world)?.count(), 2); // first run: everything is new
///
/// if let Some(health) = world.get_mut::<Health>(b) {
///     health.0 -= 3;
/// }
/// assert_eq!(hurt.iter(&world)?.collect::<Vec<_>>(), [b]);
/// assert_eq!(hurt.iter(&world)?.count(), 0);
/// # Ok::<(), pragma_ecs::EcsError>(())
/// ```
pub struct Changed<T: Component>(PhantomData<fn() -> T>);

impl<T: Component> fmt::Debug for Changed<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Changed<{}>", type_name::<T>())
    }
}

impl<T: Component> Sealed for Changed<T> {}

impl<T: Component> QueryFilter for Changed<T> {
    tick_filter!(changed);
}

#[cfg(test)]
mod tests {
    use super::*;

    fn column(values: &[u32]) -> ColumnData<u32> {
        let mut column = ColumnData::default();
        for (tick, value) in (1..).zip(values) {
            column.push(*value, tick, tick);
        }
        column
    }

    #[test]
    fn a_shared_column_is_not_handed_out_for_writing() {
        let mut values = column(&[1, 2]);
        let mut column = Some(ColumnRef::Unique(&mut values));
        assert_eq!(take_shared::<u32>(&mut column), Some(&[1, 2][..]));
        assert_eq!(take_shared::<u32>(&mut column), Some(&[1, 2][..]));
        assert!(take_unique::<u32>(&mut column).is_none());
        assert!(matches!(column, Some(ColumnRef::Shared(_))));
        assert!(take_shared::<u64>(&mut column).is_none());
    }

    #[test]
    fn option_slices_split_with_and_without_a_column() {
        let values = [1, 2, 3];
        let present = OptionSlice {
            values: Some(&values[..]),
            len: 3,
        };
        let (left, right) = <Option<&i32> as QueryData>::split_at(present, 2);
        let left: Vec<_> = <Option<&i32> as QueryData>::iter(left).collect();
        let right: Vec<_> = <Option<&i32> as QueryData>::iter(right).collect();
        assert_eq!(left, [Some(&1), Some(&2)]);
        assert_eq!(right, [Some(&3)]);

        let absent = OptionSlice::<i32> {
            values: None,
            len: 3,
        };
        let (left, right) = <Option<&i32> as QueryData>::split_at(absent, 5);
        assert_eq!(<Option<&i32> as QueryData>::iter(left).count(), 3);
        assert_eq!(
            <Option<&i32> as QueryData>::iter(right).size_hint(),
            (0, Some(0))
        );
    }

    #[test]
    fn tick_filters_clear_failing_rows() {
        let ticks = column(&[7, 8, 9]);
        let lookup = |ty: TypeId| (ty == TypeId::of::<u32>()).then(|| ticks.ticks());
        let mut mask = [true, true, false];
        <Changed<u32> as QueryFilter>::mask(&lookup, 1, &mut mask);
        assert_eq!(mask, [false, true, false]);
        let mut mask = [true; 3];
        <(Added<u32>, With<u8>) as QueryFilter>::mask(&lookup, 2, &mut mask);
        assert_eq!(mask, [false, false, true]);
        // A missing column clears everything.
        let mut mask = [true; 3];
        <Changed<u64> as QueryFilter>::mask(&lookup, 0, &mut mask);
        assert_eq!(mask, [false; 3]);
        const { assert!(<(With<u8>, Without<u16>) as QueryFilter>::DENSE) };
        const { assert!(!<(With<u8>, Changed<u16>) as QueryFilter>::DENSE) };
        assert_eq!(format!("{:?}", Added::<u8>(PhantomData)), "Added<u8>");
    }
}