edict 0.6.1

Experimental entity-component-system 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
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
//! Flow module provides API to create high-level async workflows on top of the Edict ECS.
//!
//! Typical example would be a flow that causes an entity to move towards a target.
//! It is resolved when the entity reaches the target or target is destroyed.
//!
//! Spawning a flow also returns a handle that can be used to await or cancel the flow.
//! Spawned flow wraps flow result in a `Result` type where `Err` signals that the flow was cancelled.
//! Flows are canceled when handle is dropped or entity is despawned.
//!
//! # Example
//!
//! ```ignore
//! unit.move_to(target).await
//! ```
//!

use core::{
    any::TypeId,
    cell::UnsafeCell,
    future::Future,
    mem::ManuallyDrop,
    pin::Pin,
    task::{Context, Poll, Waker},
};

use alloc::task::Wake;

use alloc::sync::Arc;
use amity::{flip_queue::FlipQueue, ring_buffer::RingBuffer};
use hashbrown::HashMap;
use slab::Slab;

use crate::{
    system::State,
    tls, type_id,
    world::{World, WorldLocal},
    EntityId,
};

mod entity;
mod world;

pub use self::{entity::*, world::*};

/// Task that access world when polled.
pub trait Flow: 'static {
    unsafe fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>;
}

pub trait IntoFlow: 'static {
    type Flow: Flow;

    /// Converts flow into the inner flow type.
    unsafe fn into_flow(self) -> Self::Flow;
}

/// Call only from flow context.
///
/// This is public for use by custom flows.
/// Built-in flows use it internally from `FlowWorld` and `FlowEntity`.
#[inline(always)]
pub unsafe fn flow_world_ref<'a>() -> &'a WorldLocal {
    unsafe { tls::get_world_ref() }
}

/// Call only from flow context.
///
/// This is public for use by custom flows.
/// Built-in flows use it internally from `FlowWorld` and `FlowEntity`.
#[inline(always)]
pub unsafe fn flow_world_mut<'a>() -> &'a mut WorldLocal {
    unsafe { tls::get_world_mut() }
}

/// Type-erased array of newly inserted flows of a single type.
trait AnyIntoFlows {
    /// Returns type of the IntoFlow.
    fn flow_id(&self) -> TypeId;

    /// Drains the array into the queue.
    fn drain(&mut self, flows: &mut HashMap<TypeId, AnyQueue>);
}

impl<'a> dyn AnyIntoFlows + 'a {
    #[inline(always)]
    unsafe fn downcast_mut<F: 'static>(&mut self) -> &mut TypedIntoFlows<F> {
        debug_assert_eq!(self.flow_id(), type_id::<F>());

        unsafe { &mut *(self as *mut Self as *mut TypedIntoFlows<F>) }
    }
}

impl<'a> dyn AnyIntoFlows + Send + 'a {
    #[inline(always)]
    unsafe fn downcast_mut<F: 'static>(&mut self) -> &mut TypedIntoFlows<F> {
        debug_assert_eq!(self.flow_id(), type_id::<F>());

        unsafe { &mut *(self as *mut Self as *mut TypedIntoFlows<F>) }
    }
}

/// Typed array of newly inserted flows of a single type.
struct TypedIntoFlows<F> {
    array: Vec<F>,
}

impl<F> AnyIntoFlows for TypedIntoFlows<F>
where
    F: IntoFlow,
{
    fn flow_id(&self) -> TypeId {
        type_id::<F>()
    }

    fn drain(&mut self, flows: &mut HashMap<TypeId, AnyQueue>) {
        // Short-circuit if there are no new flows.
        if self.array.is_empty() {
            return;
        }

        let flow_id = type_id::<F::Flow>();

        // Find queue for this type of flows or create new one.
        let queue = flows
            .entry(flow_id)
            .or_insert_with(AnyQueue::new::<F::Flow>);

        // Safety: TypedFlows<F> is at index `type_id::<F>()` in `flows.map`.
        let typed_flows = unsafe { queue.flows.downcast_mut::<F::Flow>() };

        // Reserve space to ensure oom can't happen in the loop below.
        typed_flows.array.reserve(self.array.len());

        for into_flow in self.array.drain(..) {
            let id = typed_flows.array.vacant_key();

            let task = FlowTask {
                flow: UnsafeCell::new(ManuallyDrop::new(unsafe { into_flow.into_flow() })),
                id,
                queue: queue.queue.clone(),
            };

            typed_flows.array.insert(Arc::new(task));
            queue.ready.push(id);
        }
    }
}

struct NewSendFlows {
    map: HashMap<TypeId, Box<dyn AnyIntoFlows + Send>>,
}

impl Default for NewSendFlows {
    fn default() -> Self {
        Self::new()
    }
}

impl NewSendFlows {
    fn new() -> Self {
        NewSendFlows {
            map: HashMap::new(),
        }
    }

    pub fn typed_new_flows<F>(&mut self) -> &mut TypedIntoFlows<F>
    where
        F: IntoFlow + Send,
    {
        let new_flows = self
            .map
            .entry(type_id::<F>())
            .or_insert_with(|| Box::new(TypedIntoFlows::<F> { array: Vec::new() }));

        unsafe { new_flows.downcast_mut::<F>() }
    }

    pub fn add<F>(&mut self, flow: F)
    where
        F: IntoFlow + Send,
    {
        let typed_new_flows = self.typed_new_flows();
        typed_new_flows.array.push(flow);
    }
}

struct NewLocalFlows {
    map: HashMap<TypeId, Box<dyn AnyIntoFlows>>,
}

impl Default for NewLocalFlows {
    fn default() -> Self {
        Self::new()
    }
}

impl NewLocalFlows {
    fn new() -> Self {
        NewLocalFlows {
            map: HashMap::new(),
        }
    }

    pub fn typed_new_flows<F>(&mut self) -> &mut TypedIntoFlows<F>
    where
        F: IntoFlow,
    {
        let new_flows = self
            .map
            .entry(type_id::<F>())
            .or_insert_with(|| Box::new(TypedIntoFlows::<F> { array: Vec::new() }));

        unsafe { new_flows.downcast_mut::<F>() }
    }

    pub fn add<F>(&mut self, flow: F)
    where
        F: IntoFlow,
    {
        let typed_new_flows = self.typed_new_flows();
        typed_new_flows.array.push(flow);
    }
}

/// Trait implemented by `TypedFlows` with `F: Flow`
trait AnyFlows {
    #[cfg(debug_assertions)]
    fn flow_id(&self) -> TypeId;

    unsafe fn execute(&mut self, front: &[usize], back: &[usize]);
}

impl dyn AnyFlows {
    #[inline(always)]
    unsafe fn downcast_mut<F: 'static>(&mut self) -> &mut TypedFlows<F> {
        #[cfg(debug_assertions)]
        assert_eq!(self.flow_id(), type_id::<F>());

        unsafe { &mut *(self as *mut Self as *mut TypedFlows<F>) }
    }
}

struct FlowTask<F> {
    flow: UnsafeCell<ManuallyDrop<F>>,
    id: usize,
    queue: Arc<FlipQueue<usize>>,
}

/// Safety: `FlowTask` can be sent to another thread as `Waker`
/// which does not access `flow` field.
unsafe impl<F> Send for FlowTask<F> {}
unsafe impl<F> Sync for FlowTask<F> {}

impl<F> Wake for FlowTask<F>
where
    F: Flow,
{
    fn wake(self: Arc<Self>) {
        self.queue.push(self.id);
    }

    fn wake_by_ref(self: &Arc<Self>) {
        self.queue.push(self.id);
    }
}

impl<F> FlowTask<F>
where
    F: Flow,
{
    fn waker(self: &Arc<Self>) -> Waker {
        Waker::from(self.clone())
    }
}

/// Container of spawned flows of specific type.
struct TypedFlows<F> {
    array: Slab<Arc<FlowTask<F>>>,
}

impl<F> TypedFlows<F>
where
    F: Flow,
{
    #[inline(always)]
    unsafe fn execute(&mut self, ids: &[usize]) {
        for &id in ids {
            let Some(task) = self.array.get(id) else {
                continue;
            };

            let waker = task.waker();
            let mut cx = Context::from_waker(&waker);

            // Safety: This is the only code that can access `task.flow`.
            // It is destroyed in-place when it is ready or TypedFlows is dropped.
            let poll = unsafe {
                let pinned = Pin::new_unchecked(&mut **task.flow.get());
                unsafe { pinned.poll(&mut cx) }
            };

            if let Poll::Ready(()) = poll {
                let task = self.array.remove(id);
                // Safety: Removed from array. `task.flow` is inaccessible anywhere but here.
                unsafe {
                    ManuallyDrop::drop(&mut *task.flow.get());
                }
            }
        }
    }
}

impl<F> AnyFlows for TypedFlows<F>
where
    F: Flow,
{
    #[cfg(debug_assertions)]
    fn flow_id(&self) -> TypeId {
        type_id::<F>()
    }

    unsafe fn execute(&mut self, front: &[usize], back: &[usize]) {
        self.execute(front);
        self.execute(back);
    }
}

/// Queue of flows of a single type.
struct AnyQueue {
    queue: Arc<FlipQueue<usize>>,
    ready: RingBuffer<usize>,
    flows: Box<dyn AnyFlows>,
}

impl AnyQueue {
    fn new<F: Flow>() -> Self {
        AnyQueue {
            queue: Arc::new(FlipQueue::new()),
            ready: RingBuffer::new(),
            flows: Box::new(TypedFlows::<F> { array: Slab::new() }),
        }
    }
}

/// Flows container manages running flows,
/// collects spawned flows and executes them.
pub struct Flows {
    new_flows: NewSendFlows,
    new_local_flows: NewLocalFlows,
    map: HashMap<TypeId, AnyQueue>,
}

impl Default for Flows {
    fn default() -> Self {
        Self::new()
    }
}

impl Flows {
    pub fn new() -> Self {
        Flows {
            new_flows: NewSendFlows::new(),
            new_local_flows: NewLocalFlows::new(),
            map: HashMap::new(),
        }
    }

    /// Call at least once prior to spawning flows.
    pub fn init(world: &mut World) {
        world.with_resource(NewSendFlows::new);
        world.with_resource(NewLocalFlows::new);
    }

    fn collect_new_flows<'a>(&mut self, world: &'a mut World) -> Option<tls::Guard<'a>> {
        let world = world.local();

        let mut new_flows_res = match world.get_resource_mut::<NewSendFlows>() {
            None => return None,
            Some(new_flows) => new_flows,
        };

        std::mem::swap(&mut self.new_flows, &mut *new_flows_res);
        drop(new_flows_res);

        let mut new_local_flows_res = match world.get_resource_mut::<NewLocalFlows>() {
            None => return None,
            Some(new_local_flows) => new_local_flows,
        };

        std::mem::swap(&mut self.new_local_flows, &mut *new_local_flows_res);
        drop(new_local_flows_res);

        let guard = tls::Guard::new(world);

        // First swap all queues with ready buffer.
        for typed in self.map.values_mut() {
            debug_assert!(typed.ready.is_empty());
            typed.queue.swap_buffer(&mut typed.ready);
        }

        // Then drain all new flows into queues.
        // New flow ids are added to ready buffer.
        for (_, typed) in &mut self.new_flows.map {
            typed.drain(&mut self.map);
        }
        for (_, typed) in &mut self.new_local_flows.map {
            typed.drain(&mut self.map);
        }

        Some(guard)
    }

    pub fn execute(&mut self, world: &mut World) {
        let Some(_guard) = self.collect_new_flows(world) else {
            return;
        };

        // Execute all ready flows.
        for typed in self.map.values_mut() {
            let (front, back) = typed.ready.as_slices();
            unsafe {
                typed.flows.execute(front, back);
            }

            // Clear ready buffer.
            typed.ready.clear();
        }
    }
}

/// System that executes flows spawned in the world.
pub fn flows_system(world: &mut World, mut flows: State<Flows>) {
    let flows = &mut *flows;
    flows.execute(world);
}

/// Spawn a flow into the world.
///
/// The flow will be polled by the `flows_system`.
pub fn spawn<F>(world: &World, flow: F)
where
    F: IntoFlow + Send,
{
    world.expect_resource_mut::<NewSendFlows>().add(flow);
}

/// Spawn a flow into the world.
///
/// The flow will be polled by the `flows_system`.
pub fn spawn_local<F>(world: &WorldLocal, flow: F)
where
    F: IntoFlow,
{
    world.expect_resource_mut::<NewLocalFlows>().add(flow);
}

/// Spawn a flow for the entity into the world.
///
/// The flow will be polled by the `flows_system`.
pub fn spawn_for<F>(world: &World, id: EntityId, flow: F)
where
    F: IntoEntityFlow + Send,
{
    struct AdHoc<F> {
        id: EntityId,
        f: F,
    }

    impl<F> IntoFlow for AdHoc<F>
    where
        F: IntoEntityFlow,
    {
        type Flow = F::Flow;

        unsafe fn into_flow(self) -> F::Flow {
            unsafe { self.f.into_entity_flow(self.id) }
        }
    }

    spawn(world, AdHoc { id, f: flow });
}

/// Spawn a flow for the entity into the world.
///
/// The flow will be polled by the `flows_system`.
pub fn spawn_local_for<F>(world: &WorldLocal, id: EntityId, flow: F)
where
    F: IntoEntityFlow,
{
    struct AdHoc<F> {
        id: EntityId,
        f: F,
    }

    impl<F> IntoFlow for AdHoc<F>
    where
        F: IntoEntityFlow,
    {
        type Flow = F::Flow;

        unsafe fn into_flow(self) -> F::Flow {
            unsafe { self.f.into_entity_flow(self.id) }
        }
    }

    spawn_local(world, AdHoc { id, f: flow });
}

/// Spawns code block as a flow.
#[macro_export]
macro_rules! spawn_block {
    (in $world:ident -> $($closure:tt)*) => {
        $crate::flow::spawn(&$world, $crate::flow_closure!(|$world: &mut $crate::flow::FlowWorld| { $($closure)* }));
    };
    (in $world:ident for $entity:ident -> $($closure:tt)*) => {
        $crate::flow::spawn_for(&$world, $entity, $crate::flow_closure_for!(|mut $entity| { $($closure)* }));
    };
    (for $entity:ident in $world:ident -> $($closure:tt)*) => {
        $crate::flow::spawn_for(&$world, $entity, $crate::flow_closure_for!(|mut $entity| { $($closure)* }));
    };
    (local $world:ident -> $($closure:tt)*) => {
        $crate::flow::spawn_local(&$world, $crate::flow_closure!(|$world: &mut $crate::flow::FlowWorld| { $($closure)* }));
    };
    (local $world:ident for $entity:ident -> $($closure:tt)*) => {
        $crate::flow::spawn_local_for(&$world, $entity, $crate::flow_closure_for!(|mut $entity| { $($closure)* }));
    };
    (for $entity:ident local $world:ident -> $($closure:tt)*) => {
        $crate::flow::spawn_local_for(&$world, $entity, $crate::flow_closure_for!(|mut $entity| { $($closure)* }));
    };
    (for $entity:ident -> $($closure:tt)*) => {{
        let e = $entity.id();
        let w = $entity.get_world();
        $crate::flow::spawn_local_for(w, e, $crate::flow_closure_for!(|mut $entity| { $($closure)* }));
    }};
}

pub struct YieldNow {
    yielded: bool,
}

impl YieldNow {
    pub fn new() -> Self {
        YieldNow { yielded: false }
    }
}

impl Future for YieldNow {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let me = self.get_mut();
        if !me.yielded {
            me.yielded = true;
            cx.waker().wake_by_ref();
            Poll::Pending
        } else {
            Poll::Ready(())
        }
    }
}

#[macro_export]
macro_rules! yield_now {
    () => {
        $crate::private::YieldNow::new().await
    };
}