diff --git a/src/cc.rs b/src/cc.rs index 0b45f1a..7fbef14 100644 --- a/src/cc.rs +++ b/src/cc.rs @@ -377,6 +377,12 @@ impl RawCc { pub fn strong_count(&self) -> usize { self.ref_count() } + + /// Returns `true` if the two `Cc`s point to the same allocation + #[inline] + pub fn ptr_eq(this: &Self, other: &Self) -> bool { + this.0.as_ptr() == other.0.as_ptr() + } } impl RawWeak { @@ -412,6 +418,12 @@ impl RawWeak { pub fn weak_count(&self) -> usize { self.inner().weak_count() } + + /// Returns `true` if the two `Weak`s point to the same allocation + #[inline] + pub fn ptr_eq(this: &Self, other: &Self) -> bool { + this.0.as_ptr() == other.0.as_ptr() + } } impl RawCc { diff --git a/src/tests.rs b/src/tests.rs index 86b6e36..52a0dee 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,6 +1,6 @@ -use crate::debug; use crate::testutil::test_small_graph; use crate::{collect, Cc, Trace, Tracer}; +use crate::{debug, Weak}; use std::cell::Cell; use std::cell::RefCell; use std::ops::Deref; @@ -509,6 +509,27 @@ fn test_trace_impl_double_visits() { } } +#[test] +fn test_cc_ptr_eq() { + let a = Cc::new(1); + let b = a.clone(); + let c = Cc::new(1); + + assert!(Cc::ptr_eq(&a, &b)); + assert!(!Cc::ptr_eq(&a, &c)); +} + +#[test] +fn test_weak_ptr_eq() { + let a = Cc::new(1); + let b = a.downgrade(); + let a = a.downgrade(); + let c = Cc::new(1).downgrade(); + + assert!(Weak::ptr_eq(&a, &b)); + assert!(!Weak::ptr_eq(&a, &c)); +} + #[cfg(not(miri))] quickcheck::quickcheck! { fn test_quickcheck_16_vertex_graph(edges: Vec, atomic_bits: u16, collect_bits: u16) -> bool {