flax 0.7.1

An ergonomic archetypical ECS
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
use core::{fmt::Debug, marker::PhantomData};

use alloc::vec::Vec;

use crate::{
    archetype::{Archetype, ArchetypeId, Slice, Slot},
    system::Access,
    Entity, Fetch, FetchItem,
};

use super::{FetchAccessData, FetchPrepareData, PreparedFetch, RandomFetch};

pub trait FetchSource {
    fn resolve<'a, 'w, Q: Fetch<'w>>(
        &self,
        fetch: &Q,
        data: FetchAccessData<'a>,
    ) -> Option<(ArchetypeId, &'a Archetype, Option<Slot>)>;

    fn describe(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
}

/// Selects the fetch value from the first target of the specified relation
pub struct FromRelation {
    pub(crate) relation: Entity,
    pub(crate) name: &'static str,
}

impl FetchSource for FromRelation {
    fn resolve<'a, 'w, Q: Fetch<'w>>(
        &self,
        fetch: &Q,
        data: FetchAccessData<'a>,
    ) -> Option<(ArchetypeId, &'a Archetype, Option<Slot>)> {
        for (key, _) in data.arch.relations_like(self.relation) {
            let target = key.target.unwrap();

            let loc = data
                .world
                .location(target)
                .expect("Relation contains invalid entity");

            let arch = data.world.archetypes.get(loc.arch_id);

            if fetch.filter_arch(FetchAccessData {
                arch,
                arch_id: loc.arch_id,
                ..data
            }) {
                return Some((loc.arch_id, arch, Some(loc.slot)));
            }
        }

        None
    }

    fn describe(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl FetchSource for Entity {
    fn resolve<'a, 'w, Q: Fetch<'w>>(
        &self,
        _fetch: &Q,
        data: FetchAccessData<'a>,
    ) -> Option<(ArchetypeId, &'a Archetype, Option<Slot>)> {
        let loc = data.world.location(*self).ok()?;

        Some((
            loc.arch_id,
            data.world.archetypes.get(loc.arch_id),
            Some(loc.slot),
        ))
    }

    fn describe(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.fmt(f)
    }
}

/// Traverse the edges of a relation recursively to find the first entity which matches the fetch
pub struct Traverse {
    pub(crate) relation: Entity,
}

fn traverse_resolve<'a, 'w, Q: Fetch<'w>>(
    relation: Entity,
    fetch: &Q,
    data: FetchAccessData<'a>,
) -> Option<(ArchetypeId, &'a Archetype, Option<Slot>)> {
    let mut stack = Vec::new();
    stack.push((data.arch_id, None));
    while let Some((arch_id, slot)) = stack.pop() {
        let data = FetchAccessData {
            arch_id,
            arch: data.world.archetypes.get(arch_id),
            world: data.world,
        };

        if fetch.filter_arch(data) {
            return (arch_id, data.arch, slot).into();
        }

        for (key, _) in data.arch.relations_like(relation) {
            let target = key.target.unwrap();

            let loc = data
                .world
                .location(target)
                .expect("Relation contains invalid entity");

            stack.push((loc.arch_id, Some(loc.slot)))
        }
    }

    None
}
impl FetchSource for Traverse {
    #[inline]
    fn resolve<'a, 'w, Q: Fetch<'w>>(
        &self,
        fetch: &Q,
        data: FetchAccessData<'a>,
    ) -> Option<(ArchetypeId, &'a Archetype, Option<Slot>)> {
        return traverse_resolve(self.relation, fetch, data);
    }

    fn describe(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "transitive({})", self.relation)
    }
}

/// A fetch which proxies the source of the wrapped fetch.
///
/// This allows you to fetch different entities' components in tandem with the current items in a
/// fetch.
///
/// As an explicit source means the same item may be returned for each in the fetch Q must be read
/// only, so that the returned items can safely alias. Additionally, this reduces collateral damage
/// as it forces mutation to be contained to the currently iterated entity (mostly).
pub struct Source<Q, S> {
    fetch: Q,
    source: S,
}

impl<Q, S> Source<Q, S> {
    /// Creates a new source fetch
    pub const fn new(fetch: Q, source: S) -> Self {
        Self { fetch, source }
    }
}

impl<'q, Q, S> FetchItem<'q> for Source<Q, S>
where
    Q: FetchItem<'q>,
{
    type Item = Q::Item;
}

impl<'w, Q, S> Fetch<'w> for Source<Q, S>
where
    Q: Fetch<'w>,
    Q::Prepared: for<'x> RandomFetch<'x>,
    S: FetchSource,
{
    const MUTABLE: bool = Q::MUTABLE;

    type Prepared = PreparedSource<'w, Q::Prepared>;

    fn prepare(&'w self, data: super::FetchPrepareData<'w>) -> Option<Self::Prepared> {
        let (arch_id, arch, slot) = self.source.resolve(&self.fetch, data.into())?;

        // Bounce to the resolved archetype
        let fetch = self.fetch.prepare(FetchPrepareData {
            arch,
            arch_id,
            old_tick: data.old_tick,
            new_tick: data.new_tick,
            world: data.world,
        })?;

        Some(PreparedSource {
            slot,
            fetch,
            _marker: PhantomData,
        })
    }

    fn filter_arch(&self, data: FetchAccessData) -> bool {
        self.source.resolve(&self.fetch, data).is_some()
    }

    fn describe(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.fetch.describe(f)?;
        write!(f, "(")?;
        self.source.describe(f)?;
        write!(f, ")")?;
        Ok(())
    }

    fn access(&self, data: FetchAccessData, dst: &mut Vec<Access>) {
        if let Some((arch_id, arch, _)) = self.source.resolve(&self.fetch, data) {
            self.fetch.access(
                FetchAccessData {
                    arch_id,
                    world: data.world,
                    arch,
                },
                dst,
            )
        }
    }
}

// impl<'w, 'q, Q> ReadOnlyFetch<'q> for PreparedSource<Q>
// where
//     Q: ReadOnlyFetch<'q>,
// {
//     unsafe fn fetch_shared(&'q self, _: crate::archetype::Slot) -> Self::Item {
//         self.fetch.fetch_shared(self.slot)
//     }
// }

impl<'w, 'q, Q> PreparedFetch<'q> for PreparedSource<'w, Q>
where
    Q: 'w + RandomFetch<'q>,
{
    type Item = Q::Item;
    const HAS_FILTER: bool = Q::HAS_FILTER;

    unsafe fn filter_slots(&mut self, slots: crate::archetype::Slice) -> crate::archetype::Slice {
        if let Some(slot) = self.slot {
            if self.fetch.filter_slots(Slice::single(slot)).is_empty() {
                Slice::new(slots.end, slots.end)
            } else {
                slots
            }
        } else {
            self.fetch.filter_slots(slots)
        }
    }

    type Chunk = (Q::Chunk, bool);

    unsafe fn create_chunk(&'q mut self, slice: crate::archetype::Slice) -> Self::Chunk {
        if let Some(slot) = self.slot {
            (self.fetch.create_chunk(Slice::single(slot)), true)
        } else {
            (self.fetch.create_chunk(slice), false)
        }
    }

    unsafe fn fetch_next(chunk: &mut Self::Chunk) -> Self::Item {
        if chunk.1 {
            Q::fetch_shared_chunk(&chunk.0, 0)
        } else {
            Q::fetch_next(&mut chunk.0)
        }
    }
}

pub struct PreparedSource<'w, Q> {
    slot: Option<Slot>,
    fetch: Q,
    _marker: PhantomData<&'w mut ()>,
}

#[cfg(test)]
mod test {
    use itertools::Itertools;

    use crate::{
        component,
        components::{child_of, name},
        entity_ids, FetchExt, Query, Topo, World,
    };

    use super::*;

    component! {
        a: u32,
        relation(id): (),
    }

    #[test]
    fn parent_fetch() {
        let mut world = World::new();

        let child_1 = Entity::builder()
            .set(name(), "child.1".into())
            .set(a(), 8)
            .spawn(&mut world);

        let root = Entity::builder()
            .set(name(), "root".into())
            .set(a(), 4)
            .spawn(&mut world);

        let child_1_1 = Entity::builder()
            .set(name(), "child.1.1".into())
            .spawn(&mut world);

        let child_2 = Entity::builder()
            .set(name(), "child.2".into())
            .spawn(&mut world);

        world.set(child_1, child_of(root), ()).unwrap();
        world.set(child_2, child_of(root), ()).unwrap();
        world.set(child_1_1, child_of(child_1), ()).unwrap();

        let mut query = Query::new((
            name().deref(),
            (name().deref(), a().copied()).relation(child_of).opt(),
        ))
        .with_strategy(Topo::new(child_of));

        pretty_assertions::assert_eq!(
            query.borrow(&world).iter().collect_vec(),
            [
                ("root", None),
                ("child.1", Some(("root", 4))),
                ("child.1.1", Some(("child.1", 8))),
                ("child.2", Some(("root", 4))),
            ]
        );
    }

    #[test]
    fn multi_parent_fetch() {
        let mut world = World::new();

        let child = Entity::builder()
            .set(name(), "child".into())
            .set(a(), 8)
            .spawn(&mut world);

        let parent = Entity::builder()
            .set(name(), "parent".into())
            .spawn(&mut world);

        let parent2 = Entity::builder()
            .set(name(), "parent2".into())
            .set(a(), 8)
            .spawn(&mut world);

        world.set(child, relation(parent), ()).unwrap();
        world.set(child, relation(parent2), ()).unwrap();

        let mut query = Query::new((
            name().deref(),
            (name().deref(), a().copied()).relation(relation).opt(),
        ))
        .with_strategy(Topo::new(relation));

        assert_eq!(
            query.borrow(&world).iter().collect_vec(),
            [
                ("parent", None),
                ("parent2", None),
                ("child", Some(("parent2", 8))),
            ]
        );
    }

    #[test]
    fn traverse() {
        let mut world = World::new();

        let root = Entity::builder()
            .set(name(), "root".into())
            .set(a(), 5)
            .spawn(&mut world);

        let root3 = Entity::builder()
            .set(name(), "root".into())
            .spawn(&mut world);

        let root2 = Entity::builder()
            .set(name(), "root2".into())
            .set(a(), 7)
            .spawn(&mut world);

        let child_1 = Entity::builder()
            .set(name(), "child_1".into())
            .set(relation(root), ())
            .spawn(&mut world);

        let _child_3 = Entity::builder()
            .set(name(), "child_3".into())
            .set(relation(root2), ())
            .spawn(&mut world);

        let _child_4 = Entity::builder()
            .set(name(), "child_4".into())
            .set(relation(root3), ())
            .spawn(&mut world);

        let _child_5 = Entity::builder()
            .set(name(), "child_5".into())
            .set(relation(root3), ())
            .set(relation(root2), ())
            .spawn(&mut world);

        let _child_2 = Entity::builder()
            .set(name(), "child_2".into())
            .set(relation(root), ())
            .spawn(&mut world);

        let _child_1_1 = Entity::builder()
            .set(name(), "child_1_1".into())
            .set(relation(child_1), ())
            .spawn(&mut world);

        let mut query = Query::new((
            name().deref(),
            (name().deref(), a().copied()).traverse(relation),
        ));

        assert_eq!(
            query.borrow(&world).iter().sorted().collect_vec(),
            [
                ("child_1", ("root", 5)),
                ("child_1_1", ("root", 5)),
                ("child_2", ("root", 5)),
                ("child_3", ("root2", 7)),
                ("child_5", ("root2", 7)),
                ("root", ("root", 5)),
                ("root2", ("root2", 7)),
            ]
        );
    }

    #[test]
    fn id_source() {
        let mut world = World::new();

        let _id1 = Entity::builder()
            .set(name(), "id1".to_string())
            .spawn(&mut world);
        let _id2 = Entity::builder()
            .set(name(), "id2".to_string())
            .spawn(&mut world);

        let id3 = Entity::builder()
            .set(name(), "id3".to_string())
            .set(a(), 5)
            .spawn(&mut world);

        let mut query = Query::new((
            name().cloned(),
            Source {
                source: id3,
                fetch: (entity_ids(), a(), name().cloned()),
            },
        ));

        assert_eq!(
            query.borrow(&world).iter().collect_vec(),
            &[
                ("id1".to_string(), (id3, &5, "id3".to_string())),
                ("id2".to_string(), (id3, &5, "id3".to_string())),
                ("id3".to_string(), (id3, &5, "id3".to_string()))
            ]
        );

        let mut query2 = Query::new((
            name().cloned(),
            Source {
                source: id3,
                fetch: (a().maybe_mut()),
            },
        ));

        for (name, id3_a) in &mut query2.borrow(&world) {
            *id3_a.write() += name.len() as u32;
        }

        use alloc::string::ToString;

        assert_eq!(
            query.borrow(&world).iter().collect_vec(),
            &[
                ("id1".to_string(), (id3, &14, "id3".to_string())),
                ("id2".to_string(), (id3, &14, "id3".to_string())),
                ("id3".to_string(), (id3, &14, "id3".to_string()))
            ]
        );
    }
}