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
#![allow(unused_mut, unused_parens)]

#[macro_export]
macro_rules! system {
    ($name:ident, |$( $v:ident : $t:ty ),*| $r:block) => {
        #[derive(Default, Clone, Copy)]
        pub struct $name;

        impl<'a> System<'a> for $name {
            type SystemData = ($(
                $t,
            )*);
            fn run(&mut self, ($(mut $v,)*): Self::SystemData) {
                $r
            }
        }
    };
    ($name:ident <
                   $( $generic:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+
                   >, |$( $v:ident : $t:ty ),*| $r:block) => {
        #[derive(Clone, Copy)]
        pub struct $name < $( $generic ),+ > {
            _phantom: std::marker::PhantomData<($( $generic ),+)>,
        }

        impl<'a, $( $generic $( : $clt $(+ $dlt )* )? ),+ > System<'a> for $name < $( $generic ),+ >{
            type SystemData = ($(
                $t,
            )*);
            fn run(&mut self, ($(mut $v,)*): Self::SystemData) {
                $r
            }
        }

        impl<$( $generic ),+> Default for $name < $( $generic ),+ >{
            fn default() -> Self {
                Self {
                    _phantom: std::marker::PhantomData,
                }
            }
        }
    };
}

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

    #[derive(Debug, Default)]
    pub struct Test {
        pub a: i32,
    }

    #[test]
    fn it_works() {
        system!(
            SystemName,
            |test: Read<'a, Test>, _test2: Write<'a, Test>| {
                println!("{}", test.a);
            }
        );
    }

    #[test]
    fn it_works2() {
        system!(
            SystemName<T: Send + Sync + 'static + Default, A>,
            |_test: Read<'a, T>| {}
        );
    }

    #[test]
    fn default_test() {
        enum E {}
        system!(SystemName<T: Send + Sync + 'static>, |_test: ReadExpect<
            'a,
            T,
        >| {});
        let _sys = SystemName::<E>::default();
    }
}