zero_ecs/
lib.rs

1pub use itertools::chain;
2pub use itertools::izip;
3pub use rayon;
4pub use rayon::prelude::*;
5pub use zero_ecs_macros::component;
6pub use zero_ecs_macros::entity;
7pub use zero_ecs_macros::system;
8#[macro_export]
9macro_rules! izip_par {
10    // @closure creates a tuple-flattening closure for .map() call. usage:
11    // @closure partial_pattern => partial_tuple , rest , of , iterators
12    // eg. izip!( @closure ((a, b), c) => (a, b, c) , dd , ee )
13    ( @closure $p:pat => $tup:expr ) => {
14        |$p| $tup
15    };
16
17    // The "b" identifier is a different identifier on each recursion level thanks to hygiene.
18    ( @closure $p:pat => ( $($tup:tt)* ) , $_iter:expr $( , $tail:expr )* ) => {
19        $crate::izip_par!(@closure ($p, b) => ( $($tup)*, b ) $( , $tail )*)
20    };
21
22    // unary
23    ($first:expr $(,)*) => {
24        $crate::IntoParallelIterator::into_par_iter($first)
25    };
26
27    // binary
28    ($first:expr, $second:expr $(,)*) => {
29        $crate::izip_par!($first)
30            .zip($second)
31    };
32
33    // n-ary where n > 2
34    ( $first:expr $( , $rest:expr )* $(,)* ) => {
35        $crate::izip_par!($first)
36            $(
37                .zip($rest)
38            )*
39            .map(
40                $crate::izip!(@closure a => (a) $( , $rest )*)
41            )
42    };
43}
44#[macro_export]
45macro_rules! chain_par {
46    () => {
47        rayon::iter::empty()
48    };
49    ($first:expr $(, $rest:expr )* $(,)?) => {
50        {
51            let iter = $crate::IntoParallelIterator::into_par_iter($first);
52            $(
53                let iter =
54                    ParallelIterator::chain(
55                        iter,
56                        $crate::IntoParallelIterator::into_par_iter($rest));
57            )*
58            iter
59        }
60    };
61}
62
63// found code for sum here: https://gist.github.com/jnordwick/1473d5533ca158d47ba4
64#[macro_export]
65macro_rules! sum {
66    ($h:expr) => ($h);              // so that this would be called, I ...
67    ($h:expr, $($t:expr),*) =>
68        (sum!($h) + sum!($($t),*)); // ...call sum! on both sides of the operation
69}