From 5da166314f71804c2c54abcb1a91ccac7866908b Mon Sep 17 00:00:00 2001 From: kvark Date: Tue, 7 Jan 2014 20:21:04 -0500 Subject: Fixed Gc::clone, implemented Gc::ptr_eq --- src/libstd/gc.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/libstd/gc.rs b/src/libstd/gc.rs index df7111ca510..1b53c70a9a1 100644 --- a/src/libstd/gc.rs +++ b/src/libstd/gc.rs @@ -18,10 +18,10 @@ collector is task-local so `Gc` is not sendable. use kinds::Send; use clone::{Clone, DeepClone}; +use managed; /// Immutable garbage-collected pointer type #[no_send] -#[deriving(Clone)] pub struct Gc { priv ptr: @T } @@ -32,14 +32,26 @@ impl Gc { pub fn new(value: T) -> Gc { Gc { ptr: @value } } -} -impl Gc { /// Borrow the value contained in the garbage-collected box #[inline] pub fn borrow<'r>(&'r self) -> &'r T { &*self.ptr } + + /// Determine if two garbage-collected boxes point to the same object + #[inline] + pub fn ptr_eq(&self, other: &Gc) -> bool { + managed::ptr_eq(self.ptr, other.ptr) + } +} + +impl Clone for Gc { + /// Clone the pointer only + #[inline] + fn clone(&self) -> Gc { + Gc{ ptr: self.ptr } + } } /// The `Send` bound restricts this to acyclic graphs where it is well-defined. @@ -92,6 +104,16 @@ mod tests { assert_eq!(*y.borrow(), 5); } + #[test] + fn test_ptr_eq() { + let x = Gc::new(5); + let y = x.clone(); + let z = Gc::new(7); + assert!(x.ptr_eq(&x)); + assert!(x.ptr_eq(&y)); + assert!(!x.ptr_eq(&z)); + } + #[test] fn test_destructor() { let x = Gc::new(~5); -- cgit 1.4.1-3-g733a5