Skip to content

Commit 563f685

Browse files
authored
Merge pull request #108 from calebsander/fix/ptr_eq
Fix ptr_eq() on rooted and unrooted references
2 parents 9b05709 + 6e1024c commit 563f685

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

gc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<T: Trace> Gc<T> {
8888

8989
/// Returns `true` if the two `Gc`s point to the same allocation.
9090
pub fn ptr_eq(this: &Gc<T>, other: &Gc<T>) -> bool {
91-
this.ptr_root.get().as_ptr() == other.ptr_root.get().as_ptr()
91+
ptr::eq(this.inner(), other.inner())
9292
}
9393
}
9494

gc/tests/gc_semantics.rs

+24
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,27 @@ fn trait_gc() {
267267
use_trait_gc(gc_foo);
268268
use_trait_gc(gc_bar);
269269
}
270+
271+
#[test]
272+
fn ptr_eq() {
273+
#[derive(Finalize, Trace)]
274+
struct A;
275+
#[derive(Finalize, Trace)]
276+
struct B(Gc<A>);
277+
278+
let a = Gc::new(A);
279+
let aa = a.clone();
280+
assert!(Gc::ptr_eq(&a, &aa));
281+
let b = Gc::new(B(aa));
282+
assert!(Gc::ptr_eq(&a, &b.0));
283+
let bb = Gc::new(B(a.clone()));
284+
assert!(Gc::ptr_eq(&b.0, &bb.0));
285+
286+
let a2 = Gc::new(A);
287+
assert!(!Gc::ptr_eq(&a, &a2));
288+
let b2 = Gc::new(B(a2.clone()));
289+
assert!(Gc::ptr_eq(&a2, &b2.0));
290+
assert!(!Gc::ptr_eq(&a, &b2.0));
291+
assert!(!Gc::ptr_eq(&b.0, &b2.0));
292+
assert!(!Gc::ptr_eq(&b.0, &a2));
293+
}

0 commit comments

Comments
 (0)