Skip to content

Commit

Permalink
Add failing test for Manishearth#124
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Oct 17, 2022
1 parent 6c5f754 commit 72b180c
Showing 1 changed file with 36 additions and 0 deletions.
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!");
}

0 comments on commit 72b180c

Please sign in to comment.