borrowscope_runtime/tracker/
statics.rs

1//! Static and const tracking
2
3use super::TRACKER;
4
5pub fn track_static_init<T>(
6    #[cfg_attr(not(feature = "track"), allow(unused_variables))] var_name: &str,
7    #[cfg_attr(not(feature = "track"), allow(unused_variables))] var_id: usize,
8    #[cfg_attr(not(feature = "track"), allow(unused_variables))] type_name: &str,
9    #[cfg_attr(not(feature = "track"), allow(unused_variables))] is_mutable: bool,
10    value: T,
11) -> T {
12    #[cfg(feature = "track")]
13    {
14        let mut tracker = TRACKER.lock();
15        tracker.record_static_init(var_name, var_id, type_name, is_mutable);
16    }
17    value
18}
19
20/// Track static variable access (read or write).
21///
22/// Records a `StaticAccess` event. Use this when reading from or writing to a static variable.
23///
24/// # Arguments
25///
26/// * `var_id` - Identifier of the static variable
27/// * `var_name` - Name of the static variable
28/// * `is_write` - `true` for writes, `false` for reads
29/// * `location` - Source location (e.g., "file.rs:42")
30#[inline(always)]
31pub fn track_static_access(
32    #[cfg_attr(not(feature = "track"), allow(unused_variables))] var_id: usize,
33    #[cfg_attr(not(feature = "track"), allow(unused_variables))] var_name: &str,
34    #[cfg_attr(not(feature = "track"), allow(unused_variables))] is_write: bool,
35    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
36) {
37    #[cfg(feature = "track")]
38    {
39        let mut tracker = TRACKER.lock();
40        tracker.record_static_access(var_id, var_name, is_write, location);
41    }
42}
43
44/// Track const evaluation.
45///
46/// Records a `ConstEval` event. Use this when a const value is evaluated.
47///
48/// # Arguments
49///
50/// * `const_name` - Name of the const
51/// * `const_id` - Unique identifier
52/// * `type_name` - Type of the const
53/// * `location` - Source location
54/// * `value` - The const value (returned unchanged)
55///
56/// # Returns
57///
58/// The input value, unchanged.
59#[inline(always)]
60pub fn track_const_eval<T>(
61    #[cfg_attr(not(feature = "track"), allow(unused_variables))] const_name: &str,
62    #[cfg_attr(not(feature = "track"), allow(unused_variables))] const_id: usize,
63    #[cfg_attr(not(feature = "track"), allow(unused_variables))] type_name: &str,
64    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
65    value: T,
66) -> T {
67    #[cfg(feature = "track")]
68    {
69        let mut tracker = TRACKER.lock();
70        tracker.record_const_eval(const_name, const_id, type_name, location);
71    }
72    value
73}