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
/// Utility macro to spawn functions.
///
/// Since a very common pattern is to pass multiple values to a spawned
/// function by using tuples it can be cumbersome to repeat the arguments.
/// This macro makes it possible to not repeat oneself.  The first argument
/// to the macro is a tuple of the arguments to pass, the second argument
/// is the closure that should be invoked.
///
/// For each argument the following expressions are possible:
///
/// * `ident`: uses the local variable `ident` unchanged.
/// * `ident => other`: serializes `ident` and deserializes into `other`
/// * `*ident`: alias for `*ident => ident`.
/// * `mut ident`: alias for `ident => mut ident`.
///
/// Example:
///
/// ```rust,no_run
/// let a = 42u32;
/// let b = 23u32;
/// let handle = procspawn::spawn!((a, mut b) || {
///     b += 1;
///     a + b
/// });
/// ```
///
/// To spawn in a pool you need to pass the pool to it (prefixed with `in`):
///
/// ```rust,no_run
/// # let pool = procspawn::Pool::new(4).unwrap();
/// let a = 42u32;
/// let b = 23u32;
/// let handle = procspawn::spawn!(in pool, (a, mut b) || {
///     b += 1;
///     a + b
/// });
/// ```
///
#[macro_export]
macro_rules! spawn {
    (in $pool:expr, $($args:tt)*) => { $crate::_spawn_impl!(pool $pool, $($args)*) };
    ($($args:tt)*) => { $crate::_spawn_impl!(func $crate::spawn, $($args)*) }
}

#[macro_export]
#[doc(hidden)]
macro_rules! _spawn_impl {
    (pool $pool:expr, () || $($body:tt)*) => {
        $pool.spawn(
            (),
            |()|
            $($body)*
        )
    };
    (pool $pool:expr, ($($param:tt)*) || $($body:tt)*) => {
        $pool.spawn(
            $crate::_spawn_call_arg!($($param)*),
            |($crate::_spawn_decl_arg!($($param)*))|
            $($body)*
        )
    };
    (func $func:path, () || $($body:tt)*) => {
        $func(
            (),
            |()|
            $($body)*
        )
    };
    (func $func:path, ($($param:tt)*) || $($body:tt)*) => {
        $func(
            $crate::_spawn_call_arg!($($param)*),
            |($crate::_spawn_decl_arg!($($param)*))|
            $($body)*
        )
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! _spawn_call_arg {
    ($expr:expr => mut $x:ident, $($tt:tt)*) => {(
        $expr, $crate::_spawn_call_arg!($($tt)*)
    )};
    (*$expr:expr => $x:ident, $($tt:tt)*) => {(
        *$expr, $crate::_spawn_call_arg!($($tt)*)
    )};
    ($expr:expr => $x:ident, $($tt:tt)*) => {(
        $expr, $crate::_spawn_call_arg!($($tt)*)
    )};
    ($expr:expr => mut $x:ident) => {(
        $expr,
    )};
    ($expr:expr => $x:ident) => {(
        $expr,
    )};
    (mut $x:ident, $($tt:tt)*) => {(
        $x, $crate::_spawn_call_arg!($($tt)*)
    )};
    (mut $x:ident) => {(
        $x,
    )};
    (*$x:ident, $($tt:tt)*) => {(
        *$x, $crate::_spawn_call_arg!($($tt)*)
    )};
    ($x:ident, $($tt:tt)*) => {(
        $x, $crate::_spawn_call_arg!($($tt)*)
    )};
    (*$x:ident) => {(
        *$x,
    )};
    ($x:ident) => {(
        $x,
    )};

    ($unexpected:tt) => {
        $crate::_spawn_unexpected($unexpected);
    };
    () => (())
}

#[macro_export]
#[doc(hidden)]
macro_rules! _spawn_decl_arg {
    ($expr:expr => mut $x:ident, $($tt:tt)*) => {(
        mut $x, $crate::_spawn_decl_arg!($($tt)*)
    )};
    (*$expr:expr => $x:ident, $($tt:tt)*) => {(
        $x, $crate::_spawn_decl_arg!($($tt)*)
    )};
    ($expr:expr => $x:ident, $($tt:tt)*) => {(
        $x, $crate::_spawn_decl_arg!($($tt)*)
    )};
    ($expr:expr => mut $x:ident) => {(
        mut $x,
    )};
    (*$expr:expr => $x:ident) => {(
        $x,
    )};
    ($expr:expr => $x:ident) => {(
        $x,
    )};
    (mut $x:ident, $($tt:tt)*) => {(
        mut $x, $crate::_spawn_decl_arg!($($tt)*)
    )};
    (mut $x:ident) => {(
        mut $x,
    )};
    (*$x:ident, $($tt:tt)*) => {(
        $x, $crate::_spawn_decl_arg!($($tt)*)
    )};
    ($x:ident, $($tt:tt)*) => {(
        $x, $crate::_spawn_decl_arg!($($tt)*)
    )};
    (*$x:ident) => {(
        $x,
    )};
    ($x:ident) => {(
        $x,
    )};

    ($unexpected:tt) => {
        $crate::_spawn_unexpected($unexpected);
    };
    () => ()
}

#[macro_export]
#[doc(hidden)]
macro_rules! _spawn_unexpected {
    () => {};
}