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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
//! This module contains some useful macros to simply //! create supervisors and children groups. /// This macro creates a new children group with the given amount of worker /// callbacks, and a closure that will be executed when a message is received. /// /// # Example /// /// ``` /// # use bastion::prelude::*; /// # fn main() { /// let children = children! { /// // the default redundancy is 1 /// redundancy: 100, /// action: |msg| { /// // do something with the message here /// }, /// }; /// /// let sp = supervisor! {}; /// let children = children! { /// supervisor: sp, /// redundancy: 10, /// action: |msg| { /// // do something with the message here /// }, /// }; /// # } /// ``` #[macro_export] macro_rules! children { ($($keys:ident: $vals:expr,)*) => { children!(@sort, 1, $crate::Callbacks::default(), |_| {}, , $($keys: $vals,)* ) }; (@sort, $_:expr, $cbs:expr, $action:expr, $($sp:expr)?, redundancy: $red:expr, $($keys:ident: $vals:expr,)*) => { children!(@sort, $red, $cbs, $action, $($sp)?, $($keys: $vals,)* ) }; (@sort, $red:expr, $_:expr, $action:expr, $($sp:expr)?, callbacks: $cbs:expr, $($keys:ident: $vals:expr,)*) => { children!(@sort, $red, $cbs, $action, $($sp)?, $($keys: $vals,)* ) }; (@sort, $red:expr, $cbs:expr, $action:expr, $($_:expr)?, supervisor: $sp:expr, $($keys:ident: $vals:expr,)*) => { children!(@sort, $red, $cbs, $action, $sp, $($keys: $vals,)* ) }; (@sort, $red:expr, $cbs:expr, $_:expr, $($sp:expr)?, action: $action:expr, $($keys:ident: $vals:expr,)*) => { children!(@sort, $red, $cbs, $action, $($sp)?, $($keys: $vals,)* ) }; (@sort, $red:expr, $cbs:expr, $action:expr, ,) => { $crate::Bastion::children(|ch| { ch .with_callbacks($cbs) .with_redundancy($red) .with_exec(|ctx: $crate::context::BastionContext| { async move { let ctx = ctx; loop { let msg = ctx.recv().await?; ($action)(msg); } } }) }).expect("failed to create children group"); }; (@sort, $red:expr, $cbs:expr, $action:expr, $sp:expr,) => { $sp.children(|ch| { ch .with_callbacks($cbs) .with_redundancy($red) .with_exec(|ctx: $crate::context::BastionContext| { async move { let ctx = ctx; loop { let msg = ctx.recv().await?; ($action)(msg); } } }) }).expect("failed to create children group"); }; } /// This macro creates a new supervisor with the given strategy and the given callbacks. /// Children can be specified by using the `children` / `child` macro. /// You can provide as many children groups as you want. Supervised supervisors are currently not /// yet supported. /// /// # Example /// ``` /// # use bastion::prelude::*; /// # fn main() { /// let sp = supervisor! { /// callbacks: Callbacks::default(), /// strategy: SupervisionStrategy::OneForAll, /// }; /// # } /// ``` #[macro_export] macro_rules! supervisor { ($($keys:ident: $vals:expr,)*) => { supervisor!(@sort, $crate::supervisor::SupervisionStrategy::OneForAll, $crate::Callbacks::default(), $($keys: $vals,)* ) }; (@sort, $strat:expr, $_:expr, callbacks: $cbs:expr, $($keys:ident: $vals:expr,)*) => { supervisor!(@sort, $strat, $cbs, $($keys: $vals,)* ) }; (@sort, $_:expr, $cbs:expr, strategy: $strat:expr, $($keys:ident: $vals:expr,)*) => { supervisor!(@sort, $strat, $cbs, $($keys: $vals,)* ) }; (@sort, $strat:expr, $cbs:expr,) => { $crate::Bastion::supervisor(|sp| { sp .with_callbacks($cbs) .with_strategy($strat) }).expect("failed to create supervisor"); }; } /// Spawns a blocking task, which will run on the blocking thread pool, /// and returns the handle. /// /// # Example /// ``` /// # use std::{thread, time}; /// # use lightproc::proc_stack::ProcStack; /// # use bastion::prelude::*; /// # fn main() { /// let task = blocking! { /// thread::sleep(time::Duration::from_millis(3000)); /// }; /// run!(task); /// # } /// ``` #[macro_export] macro_rules! blocking { ($($tokens:tt)*) => { bastion_executor::blocking::spawn_blocking(async move { $($tokens)* }, lightproc::proc_stack::ProcStack::default()) }; } /// This macro blocks the current thread until passed /// future is resolved with an output (including the panic). /// /// # Example /// ``` /// # use bastion::prelude::*; /// # fn main() { /// let future1 = async move { /// 123 /// }; /// /// run! { /// let result = future1.await; /// assert_eq!(result, 123); /// }; /// /// let future2 = async move { /// 10 / 2 /// }; /// /// let result = run!(future2); /// assert_eq!(result, 5); /// # } /// ``` #[macro_export] macro_rules! run { ($action:expr) => { bastion_executor::run::run($action, lightproc::proc_stack::ProcStack::default()) }; ($($tokens:tt)*) => { bastion_executor::run::run(async move {$($tokens)*}, lightproc::proc_stack::ProcStack::default()) }; } /// Spawn a given future onto the executor from the global level. /// /// # Example /// ``` /// # use bastion::prelude::*; /// # fn main() { /// let handle = spawn! { /// panic!("test"); /// }; /// run!(handle); /// # } /// ``` #[macro_export] macro_rules! spawn { ($action:expr) => { bastion_executor::pool::spawn($action, lightproc::proc_stack::ProcStack::default()) }; ($($tokens:tt)*) => { bastion_executor::pool::spawn(async move {$($tokens)*}, lightproc::proc_stack::ProcStack::default()) }; }