about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorkvark <kvarkus@gmail.com>2014-01-07 20:21:04 -0500
committerkvark <kvarkus@gmail.com>2014-01-07 20:21:25 -0500
commit5da166314f71804c2c54abcb1a91ccac7866908b (patch)
tree6845367a3ededaf70649126f77ae2a8b03f244db /src
parent7613b15fdbbb9bf770a2c731f4135886b0ff3cf0 (diff)
downloadrust-5da166314f71804c2c54abcb1a91ccac7866908b.tar.gz
rust-5da166314f71804c2c54abcb1a91ccac7866908b.zip
Fixed Gc::clone, implemented Gc::ptr_eq
Diffstat (limited to 'src')
-rw-r--r--src/libstd/gc.rs28
1 files 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<T>` is not sendable.
 
 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.
@@ -93,6 +105,16 @@ mod tests {
     }
 
     #[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);
         assert_eq!(**x.borrow(), 5);