From 72b180c7cbdfd829727e96725c907e326a8127f0 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 17 Oct 2022 09:30:13 -0700 Subject: [PATCH] Add failing test for #124 Signed-off-by: Anders Kaseorg --- gc/tests/resurrection.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 gc/tests/resurrection.rs diff --git a/gc/tests/resurrection.rs b/gc/tests/resurrection.rs new file mode 100644 index 0000000..7efad46 --- /dev/null +++ b/gc/tests/resurrection.rs @@ -0,0 +1,36 @@ +use gc::{force_collect, Finalize, Gc, GcCell}; +use gc_derive::{Finalize, Trace}; + +#[derive(Finalize, Trace)] +struct Foo { + bar: GcCell>>, +} + +#[derive(Trace)] +struct Bar { + string: String, + foo: Gc, + this: GcCell>>, +} + +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!"); +}