]> git.lizzy.rs Git - rust.git/commitdiff
Fixed Gc::clone, implemented Gc::ptr_eq
authorkvark <kvarkus@gmail.com>
Wed, 8 Jan 2014 01:21:04 +0000 (20:21 -0500)
committerkvark <kvarkus@gmail.com>
Wed, 8 Jan 2014 01:21:25 +0000 (20:21 -0500)
src/libstd/gc.rs

index df7111ca5109224a905ac81712d1f6e6549272e5..1b53c70a9a1f27276fe93453ebb2287ebd2cd963 100644 (file)
 
 use kinds::Send;
 use clone::{Clone, DeepClone};
+use managed;
 
 /// Immutable garbage-collected pointer type
 #[no_send]
-#[deriving(Clone)]
 pub struct Gc<T> {
     priv ptr: @T
 }
@@ -32,14 +32,26 @@ impl<T: 'static> Gc<T> {
     pub fn new(value: T) -> Gc<T> {
         Gc { ptr: @value }
     }
-}
 
-impl<T: 'static> Gc<T> {
     /// 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<T>) -> bool {
+        managed::ptr_eq(self.ptr, other.ptr)
+    }
+}
+
+impl<T> Clone for Gc<T> {
+    /// Clone the pointer only
+    #[inline]
+    fn clone(&self) -> Gc<T> {
+        Gc{ ptr: self.ptr }
+    }
 }
 
 /// The `Send` bound restricts this to acyclic graphs where it is well-defined.
@@ -92,6 +104,16 @@ fn test_simple_clone() {
         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);