ixa/macros/schedule_relative.rs
1/// Schedules an action after a delay relative to the context's current time.
2///
3/// The scheduled action is called with the executing `context: &mut Context` as
4/// its first argument, followed by each remaining macro argument in the same
5/// order.
6///
7/// For example:
8///
9/// ```ignore
10/// schedule_relative!(context, my_delay, my_handler, arg1, arg2, arg3);
11/// ```
12///
13/// expands to code like:
14///
15/// ```ignore
16/// {
17/// let current_time = context.get_current_time();
18/// let delay: f64 = my_delay.into();
19/// let time = current_time + delay;
20/// context.add_plan(time, move |context| {
21/// my_handler(context, arg1, arg2, arg3)
22/// })
23/// }
24/// ```
25#[macro_export]
26macro_rules! schedule_relative {
27 ($context:expr, $delay:expr, $action:expr $(, $arg:expr)* $(,)?) => {
28 {
29 let current_time = ($context).get_current_time();
30 let delay: f64 = ::core::convert::Into::into($delay);
31 let time = current_time + delay;
32 ($context).add_plan(time, move |context| {
33 ($action)(context $(, $arg)*)
34 })
35 }
36 };
37}