Skip to content

Commit 72b180c

Browse files
committed
Add failing test for #124
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 6c5f754 commit 72b180c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

gc/tests/resurrection.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use gc::{force_collect, Finalize, Gc, GcCell};
2+
use gc_derive::{Finalize, Trace};
3+
4+
#[derive(Finalize, Trace)]
5+
struct Foo {
6+
bar: GcCell<Option<Gc<Bar>>>,
7+
}
8+
9+
#[derive(Trace)]
10+
struct Bar {
11+
string: String,
12+
foo: Gc<Foo>,
13+
this: GcCell<Option<Gc<Bar>>>,
14+
}
15+
16+
impl Finalize for Bar {
17+
fn finalize(&self) {
18+
*self.foo.bar.borrow_mut() = self.this.borrow().clone();
19+
}
20+
}
21+
22+
#[test]
23+
fn resurrection_by_finalizer() {
24+
let foo = Gc::new(Foo {
25+
bar: GcCell::new(None),
26+
});
27+
let bar = Gc::new(Bar {
28+
string: "Hello, world!".to_string(),
29+
foo: foo.clone(),
30+
this: GcCell::new(None),
31+
});
32+
*bar.this.borrow_mut() = Some(bar.clone());
33+
drop(bar);
34+
force_collect();
35+
assert_eq!(foo.bar.borrow().as_ref().unwrap().string, "Hello, world!");
36+
}

0 commit comments

Comments
 (0)