forked from Manishearth/rust-gc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace_impl.rs
78 lines (68 loc) · 1.33 KB
/
trace_impl.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
use gc::{Finalize, Trace};
use std::cell::RefCell;
use std::rc::Rc;
thread_local!(static X: RefCell<u8> = RefCell::new(0));
#[derive(Copy, Clone, Finalize)]
struct Foo;
unsafe impl Trace for Foo {
unsafe fn trace(&self) {
X.with(|x| {
let mut m = x.borrow_mut();
*m += 1;
});
}
unsafe fn root(&self) {}
unsafe fn unroot(&self) {}
fn finalize_glue(&self) {}
}
#[derive(Trace, Clone, Finalize)]
struct Bar {
inner: Foo,
}
#[derive(Trace, Clone, Finalize)]
struct InnerBoxSlice {
inner: Box<[u32]>,
}
#[derive(Trace, Clone, Finalize)]
struct InnerBoxStr {
inner: Box<str>,
}
#[derive(Trace, Clone, Finalize)]
struct InnerRcStr {
inner: Rc<str>,
}
#[derive(Trace, Finalize)]
struct Baz {
a: Bar,
b: Bar,
}
#[test]
fn test() {
unsafe {
InnerBoxSlice {
inner: Box::new([1, 2, 3]),
}
.trace();
InnerBoxStr {
inner: "abc".into(),
}
.trace();
InnerRcStr {
inner: "abc".into(),
}
.trace();
}
let bar = Bar { inner: Foo };
unsafe {
bar.trace();
}
X.with(|x| assert!(*x.borrow() == 1));
let baz = Baz {
a: bar.clone(),
b: bar,
};
unsafe {
baz.trace();
}
X.with(|x| assert!(*x.borrow() == 3));
}