rune/core/gc/
trace.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
use super::super::object::RawObj;
use crate::core::object::{Gc, Object};
use rune_core::hashmap::{HashMap, HashSet};

pub(crate) trait Trace {
    fn trace(&self, state: &mut GcState);
}

pub(crate) struct GcState {
    stack: Vec<RawObj>,
    pub(in crate::core) to_space: bumpalo::Bump,
}

impl GcState {
    pub fn new() -> Self {
        GcState { stack: Vec::new(), to_space: bumpalo::Bump::new() }
    }

    pub fn push(&mut self, obj: Object) {
        self.stack.push(Gc::into_raw(obj));
    }

    pub fn trace_stack(&mut self) {
        while let Some(raw) = self.stack.pop() {
            let obj = unsafe { Object::from_raw(raw) };
            obj.trace(self);
        }
    }
}

impl Trace for usize {
    fn trace(&self, _: &mut GcState) {}
}

impl Trace for u64 {
    fn trace(&self, _: &mut GcState) {}
}

impl Trace for i64 {
    fn trace(&self, _: &mut GcState) {}
}

impl<T: Trace> Trace for &T {
    fn trace(&self, state: &mut GcState) {
        (*self).trace(state);
    }
}

impl<T: Trace, U: Trace> Trace for (T, U) {
    fn trace(&self, state: &mut GcState) {
        self.0.trace(state);
        self.1.trace(state);
    }
}

impl<T: Trace> Trace for [T] {
    fn trace(&self, state: &mut GcState) {
        for x in self {
            x.trace(state);
        }
    }
}

impl<T: Trace, const N: usize> Trace for [T; N] {
    fn trace(&self, state: &mut GcState) {
        for x in self {
            x.trace(state);
        }
    }
}

impl<T: Trace> Trace for Vec<T> {
    fn trace(&self, state: &mut GcState) {
        for x in self {
            x.trace(state);
        }
    }
}

impl<T: Trace> Trace for std::collections::VecDeque<T> {
    fn trace(&self, state: &mut GcState) {
        for x in self {
            x.trace(state);
        }
    }
}

impl<K: Trace, V: Trace> Trace for HashMap<K, V> {
    fn trace(&self, state: &mut GcState) {
        for key in self.keys() {
            key.trace(state);
        }
        for value in self.values() {
            value.trace(state);
        }
    }
}

impl<T: Trace> Trace for HashSet<T> {
    fn trace(&self, state: &mut GcState) {
        for x in self {
            x.trace(state);
        }
    }
}

impl<T: Trace> Trace for Option<T> {
    fn trace(&self, state: &mut GcState) {
        if let Some(x) = self.as_ref() {
            x.trace(state);
        }
    }
}

#[cfg(test)]
mod test {
    use super::super::super::gc::{Context, RootSet};
    use super::*;
    use rune_core::macros::root;

    #[derive(Default)]
    struct Foo(u64);
    impl Trace for Foo {
        fn trace(&self, _state: &mut GcState) {
            assert!(self.0 == 7);
        }
    }

    #[test]
    fn test_trace_root() {
        let roots = &RootSet::default();
        let cx = &mut Context::new(roots);
        let foo = Foo(7);
        assert_eq!(roots.roots.borrow().len(), 0);
        {
            root!(_root, init(foo), cx);
            assert_eq!(roots.roots.borrow().len(), 1);
        }
        assert_eq!(roots.roots.borrow().len(), 0);
    }
}