rune/shared/
mod.rs

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
mod assert_send;
mod caller;
mod consts;
mod fixed_vec;
mod gen;

pub(crate) use self::assert_send::AssertSend;
pub(crate) use self::caller::Caller;
pub(crate) use self::consts::Consts;
pub(crate) use self::fixed_vec::FixedVec;
pub(crate) use self::gen::Gen;

/// Test whether current assertions model should panic.
#[cfg(all(debug_assertions, feature = "std"))]
mod r#impl {
    use core::fmt::{self, Write};
    use core::sync::atomic::{AtomicU8, Ordering};

    use std::env;
    use std::thread::panicking;

    pub(crate) use std::backtrace::Backtrace;

    pub(crate) enum RuneAssert {
        /// Assert should panic.
        Panic,
        /// Assert should trace.
        Trace,
        /// Assert should log an error.
        Error,
    }

    impl RuneAssert {
        /// Test if the assert is a panic.
        #[allow(unused)]
        pub(crate) fn is_panic(&self) -> bool {
            matches!(self, Self::Panic)
        }

        /// Test if the assert is a trace.
        #[allow(unused)]
        pub(crate) fn is_trace(&self) -> bool {
            matches!(self, Self::Trace)
        }
    }

    const VAR: &str = "RUNE_ASSERT";

    static ENABLED: AtomicU8 = AtomicU8::new(0);

    pub(crate) fn rune_assert() -> RuneAssert {
        let mut value = ENABLED.load(Ordering::Relaxed);

        if value == 0 {
            value = match env::var(VAR).as_deref() {
                Ok("panic") => 1,
                Ok("trace") => 2,
                _ => 3,
            };

            ENABLED.store(value, Ordering::Relaxed);
        }

        match value {
            1 if !panicking() => RuneAssert::Panic,
            2 if !panicking() => RuneAssert::Trace,
            _ => RuneAssert::Error,
        }
    }

    pub(crate) struct CaptureAt {
        at: &'static str,
        done: usize,
        string: rust_alloc::string::String,
    }

    impl CaptureAt {
        pub(crate) fn new(at: &'static str) -> Self {
            Self {
                at,
                done: 0,
                string: rust_alloc::string::String::default(),
            }
        }

        pub(crate) fn as_str(&self) -> Option<&str> {
            if self.done > 0 {
                Some(&self.string[self.done..])
            } else {
                None
            }
        }
    }

    impl Write for CaptureAt {
        #[inline]
        fn write_str(&mut self, mut s: &str) -> fmt::Result {
            if self.done > 0 {
                return Ok(());
            }

            while let Some(n) = s.find('\n') {
                self.string.push_str(&s[..n]);

                if let Some(n) = self.string.find("at ") {
                    let at = &self.string[n + 3..];

                    if at.contains(self.at) {
                        self.done = n + 3;
                        return Ok(());
                    }
                }

                self.string.clear();
                s = &s[n + 1..];
            }

            self.string.push_str(s);
            Ok(())
        }
    }

    macro_rules! _rune_diagnose {
        ($($tt:tt)*) => {
            if $crate::shared::rune_assert().is_panic() {
                panic!($($tt)*);
            } else {
                tracing::trace!($($tt)*);
            }
        };
    }

    macro_rules! _rune_trace {
        ($at:expr, $tok:expr) => {{
            if $crate::shared::rune_assert().is_trace() {
                use std::backtrace::Backtrace;
                use std::fmt::Write as _;

                let bt = Backtrace::force_capture();
                let mut at = $crate::shared::CaptureAt::new($at);

                write!(at, "{bt}").with_span($tok.span)?;

                if let Some(_line) = at.as_str() {
                    tracing::trace!("{_line}: {:?}", $tok);
                }
            }
        }};
    }

    /// A macro for logging or panicking based on the current assertions model.
    ///
    /// The assertion model can be changed from logging to panicking by setting
    /// the `RUNE_ASSERT=panic` environment.
    #[doc(inline)]
    pub(crate) use _rune_diagnose as rune_diagnose;

    /// A macro for tracing a specific call site.
    ///
    /// Tracing is enabled if `RUNE_ASSERT=trace` is set.
    #[doc(inline)]
    pub(crate) use _rune_trace as rune_trace;
}

#[cfg(not(all(debug_assertions, feature = "std")))]
mod r#impl {
    use core::fmt;

    macro_rules! _rune_diagnose {
        ($($tt:tt)*) => {
            tracing::trace!($($tt)*);
        };
    }

    macro_rules! _rune_trace {
        ($at:expr, $tok:expr) => {{
            _ = $at;
            _ = $tok;
        }};
    }

    pub(crate) struct Backtrace;

    impl Backtrace {
        #[inline(always)]
        pub(crate) fn capture() -> Self {
            Self
        }
    }

    impl fmt::Display for Backtrace {
        #[inline(always)]
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(
                f,
                "backtrace not available, missing cfg(all(debug_assertions, feature = \"std\"))"
            )
        }
    }

    /// A macro for logging or panicking based on the current assertions model.
    ///
    /// The assertion model can be changed from logging to panicking by setting
    /// the `RUNE_ASSERT=panic` environment.
    #[doc(inline)]
    pub(crate) use _rune_diagnose as rune_diagnose;

    /// A macro for tracing a specific call site.
    ///
    /// Tracing is enabled if `RUNE_ASSERT=trace` is set.
    #[doc(inline)]
    pub(crate) use _rune_trace as rune_trace;
}

pub(crate) use self::r#impl::*;