Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions gc/tests/resurrection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use gc::{force_collect, Finalize, Gc, GcCell};
use gc_derive::{Finalize, Trace};

#[derive(Finalize, Trace)]
struct Foo {
bar: GcCell<Option<Gc<Bar>>>,
}

#[derive(Trace)]
struct Bar {
string: String,
foo: Gc<Foo>,
this: GcCell<Option<Gc<Bar>>>,
}

impl Finalize for Bar {
fn finalize(&self) {
*self.foo.bar.borrow_mut() = self.this.borrow().clone();
}
}

#[test]
fn resurrection_by_finalizer() {
let foo = Gc::new(Foo {
bar: GcCell::new(None),
});
let bar = Gc::new(Bar {
string: "Hello, world!".to_string(),
foo: foo.clone(),
this: GcCell::new(None),
});
*bar.this.borrow_mut() = Some(bar.clone());
drop(bar);
force_collect();
assert_eq!(foo.bar.borrow().as_ref().unwrap().string, "Hello, world!");
}