my-ecs 0.1.2

An Entity Component System (ECS) library
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
use my_ecs::prelude::*;
use std::sync::{Arc, Mutex};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Duration;

#[test]
#[rustfmt::skip]
fn test_validate_request() {
    // Selectors and filters must be disjoint to each other in a request. Such that invalid requests
    // must be denied. Disjoint conditions are as follows.
    // 1. X's Target is different from Y's Target.
    // 2. X's All intersects Y's None or vice versa.
    // 3. X's Any is a subset of Y's None or vice versa.

    #[derive(Component)] struct Ca;
    #[derive(Component)] struct Cb;
    #[derive(Component)] struct Cc;
    #[derive(Component)] struct Cd;
    #[derive(Component)] struct Ce;
    #[derive(Component)] struct Cf;
    #[derive(Component)] struct Cx;
    #[derive(Component)] struct Cy;

    let mut ecs = Ecs::create(WorkerPool::with_len(1), [1]);

    // 1. X's Target is different from Y's Target.
    filter!(A0, Target = Cx, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    filter!(A1, Target = Cy, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(A0, A1)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Write<(A0, A1)>| {})).unwrap();

    // 2. X's All intersects Y's None or vice versa.
    filter!(B0, Target = Cx, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    filter!(B1, Target = Cx, None = Ca);
    filter!(B2, Target = Cx, None = Cb);
    filter!(B3, Target = Cx, None = (Ca, Cc));
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(B0, B1)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(B0, B2)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(B0, B3)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Write<(B0, B1)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Write<(B0, B2)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Write<(B0, B3)>| {})).unwrap();

    // 3. X's Any is a subset of Y's None or vice versa.
    filter!(C0, Target = Cx, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    filter!(C1, Target = Cx, Any = Cc);
    filter!(C2, Target = Cx, Any = Cd);
    filter!(C3, Target = Cx, Any = (Cc, Cd));
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(C0, C1)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(C0, C2)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(C0, C3)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Write<(C0, C1)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Write<(C0, C2)>| {})).unwrap();
    ecs.add_system(SystemDesc::new().with_once(|_: Write<(C0, C3)>| {})).unwrap();

    // Wrong 1. X and Y are the same.
    // Expected behavior: Read is Ok, while Write is Error.
    filter!(D0, Target = Cx, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    filter!(D1, Target = Cx, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    filter!(D2, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    // Read is Ok.
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(D0, D1)>| {})).unwrap();
    // Error in a single Write.
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Write<(D0, D1)>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));
    // Error in Read, EntWrite or Write, EntWrite.
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Read<D0>, _: EntWrite<D2>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Write<D0>, _: EntWrite<D2>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));

    // Wrong 2. X's All doesn't intersect Y's None or vice versa.
    filter!(E0, Target = Cx, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    filter!(E1, Target = Cx, None = (Cc, Cd));
    filter!(E2, None = (Cc, Cd));
    // Read is Ok.
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(E0, E1)>| {})).unwrap();
    // Error in a single Write.
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Write<(E0, E1)>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));
    // Error in Read, EntWrite or Write, EntWrite.
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Read<E0>, _: EntWrite<E2>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Write<E0>, _: EntWrite<E2>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));

    // Wrong 3. X's Any is not a subset of Y's None or vice versa.
    filter!(F0, Target = Cx, All = (Ca, Cb), None = (Cc, Cd), Any = (Ce, Cf));
    filter!(F1, Target = Cx, None = (Cc, Ce));
    filter!(F2, None = (Cc, Ce));
    // Read is Ok.
    ecs.add_system(SystemDesc::new().with_once(|_: Read<(F0, F1)>| {})).unwrap();
    // Error in a single Write.
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Write<(F0, F1)>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));
    // Error in Read, EntWrite or Write, EntWrite.
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Read<F0>, _: EntWrite<F2>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));
    let res = ecs.add_system(SystemDesc::new().with_once(|_: Write<F0>, _: EntWrite<F2>| {})).into_result();
    assert!(matches!(res, Err(EcsError::InvalidRequest(..))));
}

#[test]
#[rustfmt::skip]
fn test_mixed_reg_unreg_entity_resource() {
    // Declares components, entities, resources, selectors, and filter.

    #[derive(Component, Debug, PartialEq)] struct Ca(i32);
    #[derive(Component, Debug, PartialEq)] struct Cb(i32);
    #[derive(Component, Debug, PartialEq)] struct Cc(i32);
    #[derive(Component, Debug, PartialEq)] struct Cd(i32);
    #[derive(Entity)] struct Ea { ca: Ca }
    #[derive(Entity)] struct Eb { cb: Cb }
    #[derive(Entity)] struct Eac { ca: Ca, cc: Cc }
    #[derive(Resource)] struct Ra(i32);
    #[derive(Resource)] struct Rb(i32);
    filter!(Sa, Target = Ca);
    filter!(Sb, Target = Cb);
    filter!(Fbd, All = (Cb, Cd));

    // Declares run counter and tracking variables.

    let c = Arc::new(Mutex::new([0; 5]));
    let (c0, c1, c2, c3, c4) = (
        Arc::clone(&c), Arc::clone(&c), Arc::clone(&c), Arc::clone(&c), Arc::clone(&c),
    );

    let (r_val, w_val) = (Arc::new(Mutex::new((0, 0))), Arc::new(Mutex::new((0, 0))));
    let (rr_val, rw_val) = (Arc::new(Mutex::new(0)), Arc::new(Mutex::new(0)));
    let (ew_val_ac, ew_val_bd) = (Arc::new(Mutex::new((0, 0))), Arc::new(Mutex::new((0, 0))));
    let (c_r_val, c_w_val) = (Arc::clone(&r_val), Arc::clone(&w_val));
    let (c_rr_val, c_rw_val) = (Arc::clone(&rr_val), Arc::clone(&rw_val));
    let (c_ew_val_ac, c_ew_val_bd) = (Arc::clone(&ew_val_ac), Arc::clone(&ew_val_bd));

    // Declares systems.

    let r_sys = move |r: Read<Sa>| {
        c0.lock().unwrap()[0] += 1;
        let sum = r.iter().flatten().map(|ca| ca.0).sum::<i32>();
        let num = r.iter().flatten().count() as i32;
        *c_r_val.lock().unwrap() = (sum, num)
    };
    let w_sys = move |w: Write<Sb>| {
        c1.lock().unwrap()[1] += 1;
        let sum = w.iter().flatten().map(|ca| ca.0).sum::<i32>();
        let num = w.iter().flatten().count() as i32;
        *c_w_val.lock().unwrap() = (sum, num)
    };
    let rr_sys = move |rr: ResRead<Ra>| {
        c2.lock().unwrap()[2] += 1;
        *c_rr_val.lock().unwrap() = rr.0;
    };
    let rw_sys = move |rw: ResWrite<Rb>| {
        c3.lock().unwrap()[3] += 1;
        *c_rw_val.lock().unwrap() = rw.0;
    };
    let ew_sys = move |mut ew: EntWrite<(Eac, Fbd)>| {
        c4.lock().unwrap()[4] += 1;
        if let Some(eac_cont) = ew.0.iter_mut().next() {
            let num = eac_cont.len();
            let sum = (0..num)
                .map(|vi| {
                    let e = eac_cont.get_by_value_index(vi);
                    e.ca.0 + e.cc.0
                })
                .sum::<i32>();
            *c_ew_val_ac.lock().unwrap() = (sum, num as i32);
        }

        let (sum, num) = ew.1.iter_mut().fold((0, 0), |mut acc, cont| {
            let (sum, num) = &mut acc;

            let colb = cont.get_column_of::<Cb>().unwrap();
            *sum += colb.iter().map(|cb| cb.0).sum::<i32>();

            let cold = cont.get_column_of::<Cd>().unwrap();
            *sum += cold.iter().map(|cd| cd.0).sum::<i32>();

            *num = colb.iter().count() as i32;
            assert_eq!(*num, cold.iter().count() as i32);

            acc
        });
        *c_ew_val_bd.lock().unwrap() = (sum, num);
    };

    // Adds systems.
    // Expected behavior:
    // - r_sys(Sa), w_sys(Sb), ew_sys(Eac, Fbd): Success
    // - rr_sys(Ra), rw_sys(Rb): Fail because of not registered resources.

    let mut ecs = Ecs::create(WorkerPool::with_len(1), [1]);

    ecs.add_system(SystemDesc::new().with_system(r_sys)).unwrap();
    ecs.add_system(SystemDesc::new().with_system(w_sys)).unwrap();
    ecs.add_system(SystemDesc::new().with_system(ew_sys)).unwrap();

    let rr_sys = match ecs.add_system(SystemDesc::new().with_system(rr_sys)).into_result()
    {
        Err(EcsError::UnknownResource(_, data)) => data.take_system(),
        _ => panic!()
    };
    let rw_sys = match ecs.add_system(SystemDesc::new().with_system(rw_sys)).into_result()
    {
        Err(EcsError::UnknownResource(_, data)) => data.take_system(),
        _ => panic!()
    };

    // Adds resources then adds failed systems again.
    // Expected behavior:
    // - rr_sys(Ra), rw_sys(Rb): Success

    ecs.add_resource(Ra(10)).unwrap();
    ecs.add_system(SystemDesc::new().with_system(rr_sys)).unwrap();

    ecs.add_resource(Rb(20)).unwrap();
    ecs.add_system(SystemDesc::new().with_system(rw_sys)).unwrap();

    // Runs ecs.
    // Expected behavior:
    // - All systems are executed

    reset_vals(&r_val, &w_val, &rr_val, &rw_val, &ew_val_ac, &ew_val_bd);
    ecs.step();
    assert_eq!(*c.lock().unwrap(), [1, 1, 1, 1, 1]);
    assert_eq!(*rr_val.lock().unwrap(), 10);
    assert_eq!(*rw_val.lock().unwrap(), 20);

    // Removes `Ra` and `Rb` then runs ecs.
    // Expected behavior:
    // - rr_sys(Ra), rw_sys(Rb): Inactivated

    ecs.remove_resource::<Ra>().unwrap();

    ecs.step();
    assert_eq!(*c.lock().unwrap(), [2, 2, 1, 2, 2]);

    ecs.remove_resource::<Rb>().unwrap();

    ecs.step();
    assert_eq!(*c.lock().unwrap(), [3, 3, 1, 2, 3]);

    // Registers and adds `Ea` and `Eb` then runs ecs.
    // Expected behavior:
    // - r_sys(Sa), w_sys(Sb): Put expected (sum, num).

    let ei_a = ecs.register_entity_of::<Ea>().unwrap();
    let eid_a = ecs.add_entity(ei_a, Ea { ca: Ca(1) }).unwrap();
    let ei_b = ecs.register_entity_of::<Eb>().unwrap();
    let eid_b = ecs.add_entity(ei_b, Eb { cb: Cb(2) }).unwrap();

    reset_vals(&r_val, &w_val, &rr_val, &rw_val, &ew_val_ac, &ew_val_bd);
    ecs.step();
    assert_eq!(*c.lock().unwrap(), [4, 4, 1, 2, 4]);
    assert_eq!(*r_val.lock().unwrap(), (1, 1)); // (sum, num) : [1]
    assert_eq!(*w_val.lock().unwrap(), (2, 1)); // (sum, num) : [2]
    assert_eq!(*ew_val_ac.lock().unwrap(), (0, 0)); // (sum, num) : []
    assert_eq!(*ew_val_bd.lock().unwrap(), (0, 0)); // (sum, num) : []

    // Attaches `Cc` and `Cd` to `Ea` and `Eb` respectively.
    // Now they are (Ca, Cc) and (Cb, Cd).
    // Expected behavior:
    // - ew_sys(Eac, Fbd): Put expected (sum, num).

    ecs.execute_command(|cmdr| cmdr.change_entity(eid_a).attach(Cc(3)).finish()).unwrap();
    ecs.execute_command(|cmdr| cmdr.change_entity(eid_b).attach(Cd(4)).finish()).unwrap();

    reset_vals(&r_val, &w_val, &rr_val, &rw_val, &ew_val_ac, &ew_val_bd);
    ecs.step();
    assert_eq!(*c.lock().unwrap(), [5, 5, 1, 2, 5]);
    assert_eq!(*r_val.lock().unwrap(), (1, 1)); // (sum, num) : [1]
    assert_eq!(*w_val.lock().unwrap(), (2, 1)); // (sum, num) : [2]
    assert_eq!(*ew_val_ac.lock().unwrap(), (4, 1)); // (sum, num) : [(1, 3)]
    assert_eq!(*ew_val_bd.lock().unwrap(), (6, 1)); // (sum, num) : [(2, 4)]

    // Unregisters `Ea` and `Eb`.
    // Expected behavior:
    // - No change

    reset_vals(&r_val, &w_val, &rr_val, &rw_val, &ew_val_ac, &ew_val_bd);
    ecs.step();
    assert_eq!(*c.lock().unwrap(), [6, 6, 1, 2, 6]);
    assert_eq!(*r_val.lock().unwrap(), (1, 1)); // (sum, num) : [1]
    assert_eq!(*w_val.lock().unwrap(), (2, 1)); // (sum, num) : [2]
    assert_eq!(*ew_val_ac.lock().unwrap(), (4, 1)); // (sum, num) : [(1, 3)]
    assert_eq!(*ew_val_bd.lock().unwrap(), (6, 1)); // (sum, num) : [(2, 4)]

    // Unregisters entity(Ca, Cc) and entity(Cb, Cd).
    // Expected behavior:
    // - r_sys(Sa), w_sys(Sb), ew_sys(Eac, Fbd): Empty iteration.

    ecs.unregister_entity::<(Ca, Cc)>().unwrap();
    ecs.unregister_entity::<(Cb, Cd)>().unwrap();

    reset_vals(&r_val, &w_val, &rr_val, &rw_val, &ew_val_ac, &ew_val_bd);
    ecs.step();
    assert_eq!(*c.lock().unwrap(), [7, 7, 1, 2, 7]);
    assert_eq!(*r_val.lock().unwrap(), (0, 0)); // (sum, num) : []
    assert_eq!(*w_val.lock().unwrap(), (0, 0)); // (sum, num) : []
    assert_eq!(*ew_val_ac.lock().unwrap(), (0, 0)); // (sum, num) : []
    assert_eq!(*ew_val_bd.lock().unwrap(), (0, 0)); // (sum, num) : []

    fn reset_vals(
        r_val: &Arc<Mutex<(i32, i32)>>,
        w_val: &Arc<Mutex<(i32, i32)>>,
        rr_val: &Arc<Mutex<i32>>,
        rw_val: &Arc<Mutex<i32>>,
        ew_val_ac: &Arc<Mutex<(i32, i32)>>,
        ew_val_bd: &Arc<Mutex<(i32, i32)>>,
    ) {
        *r_val.lock().unwrap() = (0, 0);
        *w_val.lock().unwrap() = (0, 0);
        *rr_val.lock().unwrap() = 0;
        *rw_val.lock().unwrap() = 0;
        *ew_val_ac.lock().unwrap() = (0, 0);
        *ew_val_bd.lock().unwrap() = (0, 0);
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn test_async_wait() {
    use my_utils::test_utils::TimerFuture;

    // Without sub workers.
    let ecs = Ecs::create(WorkerPool::new(), []);
    inner(ecs);

    // With sub workers.
    let ecs = Ecs::create(WorkerPool::with_len(3), [3]);
    inner(ecs);

    // === Internal helper functions ===

    fn inner(mut ecs: EcsApp<Worker>) {
        let state = Arc::new(Mutex::new(0));
        let c_state = state.clone();

        ecs.add_system(SystemDesc::new().with_once(move |rr: ResRead<Post>| {
            rr.send_future(async move {
                // state 1: A bit of awaiting.
                *c_state.lock().unwrap() = 1;
                for millis in 1..10 {
                    TimerFuture::after(std::time::Duration::from_millis(millis)).await;
                }

                // state 2: All awaiting has done.
                *c_state.lock().unwrap() = 2;

                let c2_state = c_state.clone();
                let cmd = move |_: Ecs| {
                    // state 3: Executing command that the future generated.
                    *c2_state.lock().unwrap() = 3;
                    Ok(())
                };
                Ok(cmd)
            });
        }))
        .unwrap();

        // Waits until all tasks are executed completely.
        ecs.run(|err| panic!("{err:?}"));
        drop(ecs);

        // `state` must have reached state 3.
        assert_eq!(*state.lock().unwrap(), 3);
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn repeat_test_async_wait() {
    use my_ecs::{prelude::type_name, utils::call_timeout};
    use std::env;

    if env::var("REPEAT").is_ok() {
        let f = || test_async_wait();
        let name = type_name!(repeat_test_async_wait);
        let repeat = 50;
        let timeout = Duration::from_secs(300);
        call_timeout(f, name, repeat, timeout);
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn test_async_abort() {
    use my_utils::test_utils::TimerFuture;

    // Without sub workers.
    let ecs = Ecs::create(WorkerPool::new(), []);
    inner(ecs);

    // With sub workers.
    let ecs = Ecs::create(WorkerPool::with_len(3), [3]);
    inner(ecs);

    // === Internal helper functions ===

    fn inner(mut ecs: EcsApp<Worker>) {
        let state = Arc::new(Mutex::new(0));
        let c_state = state.clone();

        ecs.add_system(SystemDesc::new().with_once(move |rr: ResRead<Post>| {
            rr.send_future(async move {
                // state 1: reachable.
                *c_state.lock().unwrap() = 1;
                for millis in 1..10_000 {
                    TimerFuture::after(Duration::from_millis(millis)).await;
                }

                // state 2: unreachable due to aborting.
                *c_state.lock().unwrap() = 2;
            });
        }))
        .unwrap();

        // Future task may be executed a few times.
        ecs.step();

        // Aborts remaining tasks.
        drop(ecs);

        // `state` must be 1 due to aborting.
        assert_eq!(*state.lock().unwrap(), 1);
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn repeat_test_async_abort() {
    use my_ecs::{prelude::type_name, utils::call_timeout};
    use std::env;

    if env::var("REPEAT").is_ok() {
        let f = || test_async_abort();
        let name = type_name!(repeat_test_async_abort);
        let repeat = 1000;
        let timeout = Duration::from_secs(300);
        call_timeout(f, name, repeat, timeout);
    }
}