Skip to main content

canic/macros/
timer.rs

1// -----------------------------------------------------------------------------
2// Timer macros
3// -----------------------------------------------------------------------------
4
5// Perf-instrumented timer helpers that auto-label with module + function name.
6//
7// These macros wrap `TimerApi` so callers can schedule work without manually
8// threading labels. Labels are constructed as `module_path!()::function_name`.
9
10///
11/// timer
12/// Schedule a one-shot timer with an auto-generated label.
13///
14/// # Examples
15/// - `timer!(Duration::from_secs(5), do_cleanup);`
16/// - `timer!(Duration::ZERO, my_task, arg1, arg2);`
17///
18#[macro_export]
19macro_rules! timer {
20    ($delay:expr, $func:path $(, $($args:tt)*)? ) => {{
21        let label = concat!(module_path!(), "::", stringify!($func));
22        $crate::__internal::core::api::timer::TimerApi::set_lifecycle_timer(
23            $delay,
24            label,
25            $func($($($args)*)?),
26        )
27    }};
28}
29
30///
31/// timer_guarded
32/// Schedule a one-shot timer if none is already scheduled for the slot.
33/// Returns true when a new timer was scheduled.
34///
35/// # Examples
36/// - `timer_guarded!(MY_TIMER, Duration::from_secs(5), do_cleanup);`
37/// - `timer_guarded!(MY_TIMER, Duration::ZERO, my_task, arg1, arg2);`
38///
39#[macro_export]
40macro_rules! timer_guarded {
41    ($slot:path, $delay:expr, $func:path $(, $($args:tt)*)? ) => {{
42        let label = concat!(module_path!(), "::", stringify!($func));
43        $crate::__internal::core::api::timer::TimerApi::set_guarded(
44            &$slot,
45            $delay,
46            label,
47            $func($($($args)*)?),
48        )
49    }};
50}
51
52///
53/// timer_interval
54/// Schedule a repeating timer with an auto-generated label.
55///
56/// # Examples
57/// - `timer_interval!(Duration::from_secs(60), heartbeat);`
58/// - `timer_interval!(Duration::from_secs(10), tick, state.clone());`
59///
60#[macro_export]
61macro_rules! timer_interval {
62    ($interval:expr, $func:path $(, $($args:tt)*)? ) => {{
63        let label = concat!(module_path!(), "::", stringify!($func));
64        $crate::__internal::core::api::timer::TimerApi::set_interval(
65            $interval,
66            label,
67            move || $func($($($args)*)?),
68        )
69    }};
70}
71
72///
73/// timer_interval_guarded
74/// Schedule an init timer that installs a repeating timer for the slot.
75/// Returns true when a new timer was scheduled.
76///
77/// # Examples
78/// - `timer_interval_guarded!(MY_TIMER, Duration::ZERO, init_task; Duration::from_secs(60), tick);`
79/// - `timer_interval_guarded!(MY_TIMER, Duration::from_secs(2), init; Duration::from_secs(10), tick, state.clone());`
80///
81#[macro_export]
82macro_rules! timer_interval_guarded {
83    (
84        $slot:path,
85        $init_delay:expr,
86        $init_func:path $(, $($init_args:tt)*)?
87        ;
88        $interval:expr,
89        $tick_func:path $(, $($tick_args:tt)*)?
90    ) => {{
91        let init_label = concat!(module_path!(), "::", stringify!($init_func));
92        let tick_label = concat!(module_path!(), "::", stringify!($tick_func));
93
94        $crate::__internal::core::api::timer::TimerApi::set_guarded_interval(
95            &$slot,
96            $init_delay,
97            init_label,
98            move || $init_func($($($init_args)*)?),
99            $interval,
100            tick_label,
101            move || $tick_func($($($tick_args)*)?),
102        )
103    }};
104}