async_ecs/dispatcher/
run.rs

1use std::ops::Deref;
2
3use futures::future::BoxFuture;
4
5use crate::{
6    system::{AsyncSystem, DynamicSystemData, System},
7    world::World,
8};
9
10pub type ThreadRun = Box<dyn for<'a> Run<'a> + Send>;
11pub type LocalRun = Box<dyn for<'a> Run<'a>>;
12
13pub type ThreadRunAsync = Box<dyn for<'a> RunAsync<'a> + Send>;
14pub type LocalRunAsync = Box<dyn for<'a> RunAsync<'a>>;
15
16/// Trait for fetching data and running systems.
17/// Automatically implemented for systems.
18pub trait Run<'a> {
19    /// Runs the system now.
20    ///
21    /// # Panics
22    ///
23    /// Panics if the system tries to fetch resources
24    /// which are borrowed in an incompatible way already
25    /// (tries to read from a resource which is already written to or
26    /// tries to write to a resource which is read from).
27    fn run(&mut self, world: &'a World);
28}
29
30impl<'a, T> Run<'a> for T
31where
32    T: System<'a>,
33{
34    fn run(&mut self, world: &'a World) {
35        let data = T::SystemData::fetch(self.accessor().deref(), world);
36
37        self.run(data)
38    }
39}
40
41/// Trait for fetching data and running systems with async/await.
42/// Automatically implemented for systems.
43pub trait RunAsync<'a> {
44    /// Runs the system now.
45    ///
46    /// # Panics
47    ///
48    /// Panics if the system tries to fetch resources
49    /// which are borrowed in an incompatible way already
50    /// (tries to read from a resource which is already written to or
51    /// tries to write to a resource which is read from).
52    fn run(&mut self, world: &'a World) -> BoxFuture<'a, ()>;
53}
54
55impl<'a, T> RunAsync<'a> for T
56where
57    T: AsyncSystem<'a>,
58{
59    fn run(&mut self, world: &'a World) -> BoxFuture<'a, ()> {
60        let data = T::SystemData::fetch(self.accessor().deref(), world);
61
62        self.run_async(data)
63    }
64}