Skip to main content

runmat_runtime/
warning_store.rs

1use std::cell::RefCell;
2
3#[derive(Clone, Debug)]
4pub struct RuntimeWarning {
5    pub identifier: String,
6    pub message: String,
7}
8
9thread_local! {
10    static WARNINGS: RefCell<Vec<RuntimeWarning>> = const { RefCell::new(Vec::new()) };
11}
12
13pub fn push(identifier: &str, message: &str) {
14    if let Some(context) = crate::context::legacy::active() {
15        context.state().warnings.borrow_mut().push(RuntimeWarning {
16            identifier: identifier.to_string(),
17            message: message.to_string(),
18        });
19        return;
20    }
21    WARNINGS.with(|warnings| {
22        warnings.borrow_mut().push(RuntimeWarning {
23            identifier: identifier.to_string(),
24            message: message.to_string(),
25        })
26    });
27}
28
29pub fn take_all() -> Vec<RuntimeWarning> {
30    if let Some(context) = crate::context::legacy::active() {
31        return context.state().warnings.borrow_mut().drain(..).collect();
32    }
33    WARNINGS.with(|warnings| warnings.borrow_mut().drain(..).collect())
34}
35
36pub fn extend(warnings: impl IntoIterator<Item = RuntimeWarning>) {
37    if let Some(context) = crate::context::legacy::active() {
38        context.state().warnings.borrow_mut().extend(warnings);
39        return;
40    }
41    WARNINGS.with(|slot| slot.borrow_mut().extend(warnings));
42}
43
44pub fn reset() {
45    if let Some(context) = crate::context::legacy::active() {
46        context.state().warnings.borrow_mut().clear();
47        return;
48    }
49    WARNINGS.with(|warnings| warnings.borrow_mut().clear());
50}