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
// Taken from: https://users.rust-lang.org/t/dump-all-refcell-borrows/28315/4
::cfg_if::cfg_if! { if #[cfg(debug_assertions)] {
    #[derive(
        Debug,
        Clone,
        Default,
        PartialEq, Eq,
        PartialOrd, Ord,
    )]
    pub struct RefCell<T> {
        pub ref_cell: ::core::cell::RefCell<T>,

        pub last_borrow_context: ::core::cell::Cell<&'static str>,
    }

    impl<T> RefCell<T> {
        pub fn new (value: T) -> Self
        {
            RefCell {
                ref_cell: ::core::cell::RefCell::new(value),
                last_borrow_context: ::core::cell::Cell::new(""),
            }
        }
    }

    #[macro_export]
    macro_rules! borrow {(
        $wrapper:expr
    ) => ({
        let wrapper = &$wrapper;
        if let Ok(ret) = wrapper.ref_cell.try_borrow() {
            wrapper
                .last_borrow_context
                .set(concat!(
                    "was still borrowed from ",
                    file!(), ":", line!(), ":", column!(),
                    " on expression ",
                    stringify!($wrapper),
                ));
            ret
        } else {
            panic!(
                "Error, {} {}",
                stringify!($wrapper),
                wrapper.last_borrow_context.get(),
            );
        }
    })}

    #[macro_export]
    macro_rules! borrow_mut {(
        $wrapper:expr
    ) => ({
        let wrapper = &$wrapper;
        if let Ok(ret) = $wrapper.ref_cell.try_borrow_mut() {
            $wrapper
                .last_borrow_context
                .set(concat!(
                    "was still mutably borrowed from ",
                    file!(), ":", line!(), ":", column!(),
                    " on expression ",
                    stringify!($wrapper),
                ));
            ret
        } else {
            panic!(
                "Error, {} {}",
                stringify!($wrapper),
                wrapper.last_borrow_context.get(),
            );
        }
    })}

    #[macro_export]
    macro_rules! use_RefCell {() => (
        use pax_runtime_api::RefCell;
    )}
} else {
    #[macro_export]
    macro_rules! borrow {(
        $ref_cell:expr
    ) => (
        $ref_cell.borrow()
    )}

    #[macro_export]
    macro_rules! borrow_mut {(
        $ref_cell:expr
    ) => (
        $ref_cell.borrow_mut()
    )}
    #[macro_export]
    macro_rules! use_RefCell {() => (
        use ::core::cell::RefCell;
    )}
}}