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
491
492
493
494
495
496
497
498
499
500
501
502
use core::{mem, ptr, slice};

use alloc::vec::Vec;
use itertools::{Either, Itertools};

use crate::{
    archetype::{ArchetypeId, CellData, Slice, Slot},
    buffer::ComponentBuffer,
    component::{ComponentDesc, ComponentValue},
    entity::EntityLocation,
    metadata::exclusive,
    world::update_entity_loc,
    Entity, World,
};

/// Describes a modification to the components of an entity within the context of an archetype
pub(crate) trait ComponentUpdater {
    type Updated;
    /// Performs write operations against the target entity
    /// # Safety
    ///
    /// The provided `data` must be of the same type as the cell data and what will be
    /// written using `self`
    unsafe fn update(self, data: &mut CellData, slot: Slot, id: Entity, tick: u32)
        -> Self::Updated;
}

pub(crate) trait ComponentPusher {
    type Pushed;
    /// # Safety
    ///
    /// The cell **must** be extended with valid component data for the new entity.
    ///
    /// The type of `data` must match that of `self`
    unsafe fn push(self, data: &mut CellData, id: Entity, tick: u32) -> Self::Pushed;
}

pub(crate) struct FnWriter<F, T> {
    func: F,
    _marker: core::marker::PhantomData<T>,
}

impl<F, T> FnWriter<F, T> {
    pub(crate) fn new(func: F) -> Self {
        Self {
            func,
            _marker: core::marker::PhantomData,
        }
    }
}

impl<F, T, U> ComponentUpdater for FnWriter<F, T>
where
    F: FnOnce(&mut T) -> U,
{
    type Updated = U;

    unsafe fn update(self, data: &mut CellData, slot: Slot, id: Entity, tick: u32) -> U {
        let value = &mut *(data.storage.at_mut(slot).unwrap() as *mut T);
        let res = (self.func)(value);

        data.set_modified(&[id], Slice::single(slot), tick);
        res
    }
}

/// # Safety
///
/// The entity must be fully initialized and all bookkepping updated
pub unsafe trait EntityWriter {
    type Output;
    fn write(
        self,
        world: &mut World,
        id: Entity,
        loc: EntityLocation,
        tick: u32,
    ) -> (EntityLocation, Self::Output);
}

pub(crate) struct SingleComponentWriter<W> {
    desc: ComponentDesc,
    writer: W,
}

impl<W> SingleComponentWriter<W> {
    pub(crate) fn new(desc: ComponentDesc, writer: W) -> Self {
        Self { desc, writer }
    }
}

unsafe impl<W: ComponentUpdater + ComponentPusher> EntityWriter for SingleComponentWriter<W> {
    type Output = Either<W::Updated, W::Pushed>;

    fn write(
        self,
        world: &mut World,
        id: Entity,
        src_loc: EntityLocation,
        tick: u32,
    ) -> (EntityLocation, Self::Output) {
        let key = self.desc.key();

        let arch = world.archetypes.get_mut(src_loc.arch_id);

        if let Some(cell) = arch.cell_mut(key) {
            let res = unsafe {
                self.writer
                    .update(cell.data.get_mut(), src_loc.slot, id, tick)
            };

            return (src_loc, Either::Left(res));
        }

        let (src, dst, dst_id) = if let Some(&dst_id) = arch.outgoing.get(&key) {
            let (src, dst) = world
                .archetypes
                .get_disjoint(src_loc.arch_id, dst_id)
                .unwrap();
            (src, dst, dst_id)
        } else {
            // Oh no! The archetype is missing the component
            let exclusive = if self.desc.meta_ref().has(exclusive()) {
                slice::from_ref(&self.desc.key.id)
            } else {
                &[]
            };

            let (components, superset) = find_archetype_components(
                arch.cells().iter().map(|v| v.desc()),
                [self.desc],
                exclusive,
            );

            world.init_component(self.desc);
            let (dst_id, _) = world.archetypes.find_create(components.iter().copied());

            // Add a quick edge to refer to later
            let reserved_id = world.archetypes.reserved;
            let (src, dst) = world
                .archetypes
                .get_disjoint(src_loc.arch_id, dst_id)
                .unwrap();

            if superset && src_loc.arch_id != reserved_id {
                src.add_outgoing(key, dst_id);
                dst.add_incoming(key, src_loc.arch_id);
            }

            (src, dst, dst_id)
        };

        let (dst_slot, swapped) = unsafe { src.move_to(dst, src_loc.slot, |c, ptr| c.drop(ptr)) };

        // Insert the missing component
        let pushed = unsafe {
            let cell = dst
                .cell_mut(key)
                .expect("Missing component in new archetype");

            let data = cell.data.get_mut();

            self.writer.push(data, id, tick)
        };

        let dst_loc = EntityLocation {
            arch_id: dst_id,
            slot: dst_slot,
        };

        update_entity_loc(world, id, dst_loc, swapped);

        (dst_loc, Either::Right(pushed))
    }
}

/// # Safety
/// *All* components of the new slot must be initialized
pub(crate) unsafe trait MigrateEntity {
    fn migrate(
        self,
        world: &mut World,
        src_id: ArchetypeId,
        src_slot: Slot,
        tick: u32,
    ) -> (EntityLocation, Option<(Entity, Slot)>);
}

pub(crate) struct Replace<T: ComponentValue> {
    pub(crate) value: T,
}

impl<T: ComponentValue> Replace<T> {
    pub(crate) fn new(value: T) -> Self {
        Self { value }
    }
}

impl<T: ComponentValue> ComponentUpdater for Replace<T> {
    type Updated = T;

    unsafe fn update(self, data: &mut CellData, slot: Slot, id: Entity, tick: u32) -> T {
        let storage = data.storage.downcast_mut::<T>();
        let old = mem::replace(&mut storage[slot], self.value);

        data.set_modified(&[id], Slice::single(slot), tick);

        old
    }
}

impl<T: ComponentValue> ComponentPusher for Replace<T> {
    type Pushed = ();

    unsafe fn push(mut self, data: &mut CellData, id: Entity, tick: u32) {
        let slot = data.storage.len();

        data.storage.extend(&mut self.value as *mut T as *mut u8, 1);

        mem::forget(self.value);

        data.set_added(&[id], Slice::single(slot), tick);
    }
}

pub(crate) struct Missing<T: ComponentValue> {
    pub(crate) value: T,
}

impl<T: ComponentValue> ComponentUpdater for Missing<T> {
    type Updated = ();

    unsafe fn update(self, _: &mut CellData, _: Slot, _: Entity, _: u32) {}
}

impl<T: ComponentValue> ComponentPusher for Missing<T> {
    type Pushed = ();

    unsafe fn push(mut self, data: &mut CellData, id: Entity, tick: u32) {
        let slot = data.storage.len();

        data.storage.extend(&mut self.value as *mut T as *mut u8, 1);

        mem::forget(self.value);

        data.set_added(&[id], Slice::single(slot), tick);
    }
}

pub(crate) struct WriteDedup<T: ComponentValue> {
    pub(crate) value: T,
}

impl<T: ComponentValue> WriteDedup<T> {
    pub(crate) fn new(value: T) -> Self {
        Self { value }
    }
}

impl<T: ComponentValue + PartialEq> ComponentUpdater for WriteDedup<T> {
    type Updated = ();

    unsafe fn update(self, data: &mut CellData, slot: Slot, id: Entity, tick: u32) {
        let storage = data.storage.downcast_mut::<T>();
        let current = &mut storage[slot];
        if current != &self.value {
            *current = self.value;

            data.set_modified(&[id], Slice::single(slot), tick);
        }
    }
}

impl<T: ComponentValue + PartialEq> ComponentPusher for WriteDedup<T> {
    type Pushed = ();

    unsafe fn push(mut self, data: &mut CellData, id: Entity, tick: u32) {
        let slot = data.storage.len();

        data.storage.extend(&mut self.value as *mut T as *mut u8, 1);

        mem::forget(self.value);

        data.set_added(&[id], Slice::single(slot), tick);
    }
}

pub(crate) struct WriteDedupDyn {
    pub(crate) value: *mut u8,
    pub(crate) cmp: unsafe fn(*const u8, *const u8) -> bool,
}

impl ComponentUpdater for WriteDedupDyn {
    type Updated = ();

    unsafe fn update(self, data: &mut CellData, slot: Slot, id: Entity, tick: u32) {
        let desc = data.storage.desc();
        unsafe {
            let dst = data.storage.at_mut(slot).unwrap();

            if (self.cmp)(self.value, dst) {
                desc.drop(self.value);
                return;
            }

            desc.drop(dst);

            ptr::copy_nonoverlapping(self.value, dst, desc.size());
        }

        data.set_modified(&[id], Slice::single(slot), tick);
    }
}

impl ComponentPusher for WriteDedupDyn {
    type Pushed = ();

    unsafe fn push(self, data: &mut CellData, id: Entity, tick: u32) {
        let slot = data.storage.len();
        data.storage.extend(self.value, 1);

        data.set_added(&[id], Slice::single(slot), tick);
    }
}

pub(crate) struct ReplaceDyn {
    pub(crate) value: *mut u8,
}

impl ComponentUpdater for ReplaceDyn {
    type Updated = ();

    unsafe fn update(self, data: &mut CellData, slot: Slot, id: Entity, tick: u32) {
        let desc = data.storage.desc();
        unsafe {
            let dst = data.storage.at_mut(slot).unwrap();

            desc.drop(dst);

            ptr::copy_nonoverlapping(self.value, dst, desc.size());
        }

        data.set_modified(&[id], Slice::single(slot), tick);
    }
}

impl ComponentPusher for ReplaceDyn {
    type Pushed = ();

    unsafe fn push(self, data: &mut CellData, id: Entity, tick: u32) {
        let slot = data.storage.len();
        data.storage.extend(self.value, 1);

        data.set_added(&[id], Slice::single(slot), tick);
    }
}

pub(crate) struct MissingDyn {
    pub(crate) value: *mut u8,
}

impl ComponentUpdater for MissingDyn {
    type Updated = ();

    unsafe fn update(self, data: &mut CellData, _: Slot, _: Entity, _: u32) {
        let desc = data.storage.desc();
        unsafe {
            desc.drop(self.value);
        }
    }
}

impl ComponentPusher for MissingDyn {
    type Pushed = ();

    unsafe fn push(self, data: &mut CellData, id: Entity, tick: u32) {
        let slot = data.storage.len();
        data.storage.extend(self.value, 1);

        data.set_added(&[id], Slice::single(slot), tick);
    }
}

pub(crate) struct Buffered<'b> {
    pub(crate) buffer: &'b mut ComponentBuffer,
}

impl<'b> Buffered<'b> {
    pub(crate) fn new(buffer: &'b mut ComponentBuffer) -> Self {
        Self { buffer }
    }
}

unsafe impl<'b> EntityWriter for Buffered<'b> {
    type Output = ();

    fn write(
        self,
        world: &mut World,
        id: Entity,
        src_loc: EntityLocation,
        tick: u32,
    ) -> (EntityLocation, ()) {
        let mut exclusive_relations = Vec::new();

        let arch = world.archetypes.get_mut(src_loc.arch_id);
        unsafe {
            self.buffer.retain(|desc, src| {
                let key = desc.key;
                // The component exists in the current archetype
                // This implies that is it also satisfies any exclusive properties
                if let Some(cell) = arch.cell_mut(key) {
                    let data = cell.data.get_mut();

                    let dst = data.storage.at_mut(src_loc.slot).unwrap();
                    desc.drop(dst);
                    ptr::copy_nonoverlapping(src, dst, desc.size());

                    data.set_modified(&[id], Slice::single(src_loc.slot), tick);
                    false
                } else {
                    // Component does not exist yet, so defer a move

                    // Exclusive relation
                    if key.target.is_some() && desc.meta_ref().has(exclusive()) {
                        if exclusive_relations.contains(&key.id) {
                            panic!("Multiple exclusive relations");
                        }

                        exclusive_relations.push(key.id);
                    }

                    true
                }
            });
        }

        if self.buffer.is_empty() {
            return (src_loc, ());
        }

        // Add the existing components, making sure new exclusive relations are favored
        let (components, _) = find_archetype_components(
            arch.cells().iter().map(|v| v.desc()),
            self.buffer.components().copied(),
            &exclusive_relations,
        );

        for &desc in self.buffer.components() {
            world.init_component(desc);
        }

        let (dst_id, _) = world.archetypes.find_create(components);

        let (src, dst) = world
            .archetypes
            .get_disjoint(src_loc.arch_id, dst_id)
            .unwrap();

        let (dst_slot, swapped) = unsafe { src.move_to(dst, src_loc.slot, |c, ptr| c.drop(ptr)) };

        // Insert the missing components
        for (desc, src) in self.buffer.drain() {
            unsafe {
                dst.push(desc.key, src, tick);
            }
        }

        let dst_loc = EntityLocation {
            arch_id: dst_id,
            slot: dst_slot,
        };

        update_entity_loc(world, id, dst_loc, swapped);
        // world.archetypes.prune_arch(src_loc.arch_id);

        (dst_loc, ())
    }
}

fn find_archetype_components(
    current_components: impl IntoIterator<Item = ComponentDesc>,
    new_components: impl IntoIterator<Item = ComponentDesc>,
    // Subset of `new_components`
    exclusive: &[Entity],
) -> (Vec<ComponentDesc>, bool) {
    let mut superset = true;
    let res = new_components
        .into_iter()
        .chain(current_components.into_iter().filter(|v| {
            if exclusive.contains(&v.key.id) {
                superset = false;
                false
            } else {
                true
            }
        }))
        .sorted_unstable()
        .collect_vec();

    (res, superset)
}