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
use crate::*;

/// Struct used to run a system function using the world.
/// This struct is also used internally by the `Dispatcher` to create a coherent
/// execution sequence.
pub struct System {
    pub(crate) initialize: Box<dyn Fn(&mut World) + Send>,
    pub(crate) lock:
        Box<dyn Fn(*const World, *mut Vec<Box<dyn RefLifetime>>) -> SystemResult + Send>,
    pub(crate) run_fn: Box<dyn FnMut(&World) -> SystemResult + Send>,
}

impl System {
    /// Initializes the resources required to run this system inside of the
    /// provided `World`, if those resources don't already exist.
    ///
    /// This is called automatically if you use a `Dispatcher`, so in most
    /// cases it is not required to call it manually.
    pub fn initialize(&self, world: &mut World) {
        (self.initialize)(world)
    }
    /// Runs the system's function using the provided `World`'s resources.
    pub fn run(&mut self, world: &World) -> SystemResult {
        (self.run_fn)(world)
    }
}

/// Converts a function into a `System`. It is required to execute a function
/// automatically from `World`'s resources.
/// This trait is automatically implemented for functions taking 12 arguments (22 if using the
/// `big_systems` feature)
/// or less where:
/// - All arguments are immutable or mutable references.
/// - All immutable references are placed *before* all mutable references.
/// - All arguments implement `Default`.
/// - Does not use the same type twice.
/// - Returns a `SystemResult` (usually just `Ok(())`).
pub trait IntoSystem<R> {
    fn system(self) -> System;
}

macro_rules! impl_system {
    ($($id:ident,)* $(&mut $idmut:ident,)*) => {
        impl<$($id: Send + Sync,)* $($idmut: Send + Sync,)* F> IntoSystem<($(&$id,)* $(&mut $idmut,)*)> for F
        where
            $($id: Default+'static,)*
            $($idmut: Default+'static,)*
            F: Fn($(&$id,)* $(&mut $idmut,)*) -> SystemResult + 'static + Send,
        {
            fn system(self) -> System {
                System {
                    initialize: Box::new(|_world: &mut World| {
                        $(_world.initialize::<$id>();)*
                        $(_world.initialize::<$idmut>();)*
                    }),
                    lock: Box::new(|_world: *const World, _locked: *mut Vec<Box<dyn RefLifetime>>| {
                        // Unsafe: used to extend the lifetime because we need to store the
                        // reference of a value that is inside a RefCell to keep the counter
                        // incremented.
                        $(unsafe {(&mut *_locked).push(Box::new((*_world).get::<$id>()?))};)*
                        $(unsafe {(&mut *_locked).push(Box::new((*_world).get_mut::<$idmut>()?))};)*
                        Ok(())
                    }),
                    run_fn: Box::new(move |_world: &World| {
                        self($(&*_world.get::<$id>()?,)* $(&mut *_world.get_mut::<$idmut>()?),*)
                    }),
                }
            }
        }
    }
}

macro_rules! impl_system_muts {
    ($($processed:ident),*$(,)?;) => {
        impl_system!($(&mut $processed,)*);
    };
    ($($processed:ident),*$(,)?; $head:ident, $($tail:ident,)*) => {
        impl_system!($($tail,)* $head, $(&mut $processed,)*);
        impl_system_muts!($($processed,)* $head; $($tail,)*);
    }
}
macro_rules! impl_systems {
    // base case
    () => {};
    ($head:ident, $($idents:ident,)*) => {
        // recursive call
        impl_system_muts!(; $head, $($idents,)*);
        impl_systems!($($idents,)*);
    }
}

impl_system!();
#[cfg(not(feature="big_systems"))]
impl_systems!(A, B, C, D, E, G, H, I, J, K, L, M,);
// Sometimes I just hate rust. This compiles *very* slowly.
#[cfg(feature="big_systems")]
// 16
//impl_systems!(A, B, C, D, E, G, H, I, J, K, L, M, O, P, Q, R,);
// 26, 17s build time
//impl_systems!(A, B, C, D, E, G, H, I, J, K, L, M, O, P, Q, R, S, T, U, V, W, X, Y, Z, AA,);
// 22, 10s build time
impl_systems!(A, B, C, D, E, G, H, I, J, K, L, M, O, P, Q, R, S, T, U, V, W,);

#[cfg(test)]
mod tests {
    use crate::*;
    use wasm_bindgen_test::*;

    #[test]
    #[wasm_bindgen_test]
    fn convert_system() {
        let _ = generic::<u32>.system();
        fn tmp(_var1: &u32, _var2: &u64, _var3: &mut i32, _var4: &mut i64) -> SystemResult {
            Ok(())
        }
        // Technically reusing the same type is incorrect and causes a runtime panic.
        // However, there doesn't seem to be a clean way to handle type inequality in generics.
        fn tmp2(
            _var1: &u32,
            _var2: &u64,
            _var3: &mut i32,
            _var4: &mut i64,
            _var5: &mut i64,
            _var6: &mut i64,
            _var7: &mut i64,
            _var8: &mut i64,
            _var9: &mut i64,
            _var10: &mut i64,
            _var11: &mut i64,
            _var12: &mut i64,
        ) -> SystemResult {
            Ok(())
        }
        let _ = tmp.system();
        let _ = tmp2.system();
    }

    #[test]
    #[wasm_bindgen_test]
    fn system_is_send() {
        let x = 6;
        send(
            (move |_var1: &u32| {
                let _y = x;
                Ok(())
            })
            .system(),
        );
        send((|| Ok(())).system());
        send(sys.system());
    }

    fn sys(_var1: &u32) -> SystemResult {
        Ok(())
    }
    fn generic<T>(_t: &T) -> SystemResult {
        Ok(())
    }
    fn send<T: Send>(_t: T) {}

    #[test]
    #[wasm_bindgen_test]
    fn manual_system_run() {
        let mut world = World::default();
        world.initialize::<u32>();
        generic::<u32>.system().run(&world).unwrap();
    }

    #[test]
    #[wasm_bindgen_test]
    fn system_replace_resource() {
        #[derive(Default)]
        pub struct A;
        #[derive(Default)]
        pub struct B {
            x: u32,
        }
        let mut world = World::default();
        let mut my_system = (|_a: &A, b: &mut B| {
            let b2 = B { x: 45 };
            *b = b2;
            Ok(())
        })
        .system();
        my_system.initialize(&mut world);
        my_system.run(&world).unwrap();
        assert_eq!(world.get::<B>().unwrap().x, 45);
    }
}