about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-05-15 12:22:58 -0700
committerbors <bors@rust-lang.org>2013-05-15 12:22:58 -0700
commit8a15333c06b8ba491e1654c3fca3fa21d21def9b (patch)
treefaba4e5896e4f9c2f200716afa1f4d009a3daae3 /src/libstd
parentf4c88c7da0503d4eb1109599a51537f750667320 (diff)
parentcda3ac905a56dc6580429eea259143d30a7f3c02 (diff)
downloadrust-8a15333c06b8ba491e1654c3fca3fa21d21def9b.tar.gz
rust-8a15333c06b8ba491e1654c3fca3fa21d21def9b.zip
auto merge of #6498 : thestinger/rust/deep_clone, r=thestinger
This is mostly for `std::rc` and `std::arc` (but I haven't implemented it for ARC yet).

Implementing it correctly for managed boxes is *very* non-trivial. It would probably require an unholy mix of reflection and TLS.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/rc.rs78
1 files changed, 73 insertions, 5 deletions
diff --git a/src/libstd/rc.rs b/src/libstd/rc.rs
index 8cf2da3a1e8..cb0798f2a39 100644
--- a/src/libstd/rc.rs
+++ b/src/libstd/rc.rs
@@ -76,6 +76,7 @@ impl<T: Owned> Drop for Rc<T> {
 
 
 impl<T: Owned> Clone for Rc<T> {
+    /// Return a shallow copy of the reference counted pointer.
     #[inline]
     fn clone(&self) -> Rc<T> {
         unsafe {
@@ -85,9 +86,38 @@ impl<T: Owned> Clone for Rc<T> {
     }
 }
 
+impl<T: Owned + DeepClone> DeepClone for Rc<T> {
+    /// Return a deep copy of the reference counted pointer.
+    #[inline]
+    fn deep_clone(&self) -> Rc<T> {
+        Rc::new(self.borrow().deep_clone())
+    }
+}
+
 #[cfg(test)]
 mod test_rc {
     use super::*;
+    use core::cell::Cell;
+
+    #[test]
+    fn test_clone() {
+        let x = Rc::new(Cell(5));
+        let y = x.clone();
+        do x.borrow().with_mut_ref |inner| {
+            *inner = 20;
+        }
+        assert_eq!(y.borrow().take(), 20);
+    }
+
+    #[test]
+    fn test_deep_clone() {
+        let x = Rc::new(Cell(5));
+        let y = x.deep_clone();
+        do x.borrow().with_mut_ref |inner| {
+            *inner = 20;
+        }
+        assert_eq!(y.borrow().take(), 5);
+    }
 
     #[test]
     fn test_simple() {
@@ -96,7 +126,7 @@ mod test_rc {
     }
 
     #[test]
-    fn test_clone() {
+    fn test_simple_clone() {
         let x = Rc::new(5);
         let y = x.clone();
         assert_eq!(*x.borrow(), 5);
@@ -149,24 +179,26 @@ pub impl<T: Owned> RcMut<T> {
 
     /// Fails if there is already a mutable borrow of the box
     #[inline]
-    fn with_borrow(&self, f: &fn(&T)) {
+    fn with_borrow<U>(&self, f: &fn(&T) -> U) -> U {
         unsafe {
             assert!((*self.ptr).borrow != Mutable);
             let previous = (*self.ptr).borrow;
             (*self.ptr).borrow = Immutable;
-            f(&(*self.ptr).value);
+            let res = f(&(*self.ptr).value);
             (*self.ptr).borrow = previous;
+            res
         }
     }
 
     /// Fails if there is already a mutable or immutable borrow of the box
     #[inline]
-    fn with_mut_borrow(&self, f: &fn(&mut T)) {
+    fn with_mut_borrow<U>(&self, f: &fn(&mut T) -> U) -> U {
         unsafe {
             assert!((*self.ptr).borrow == Nothing);
             (*self.ptr).borrow = Mutable;
-            f(&mut (*self.ptr).value);
+            let res = f(&mut (*self.ptr).value);
             (*self.ptr).borrow = Nothing;
+            res
         }
     }
 }
@@ -200,6 +232,7 @@ impl<T: Owned> Drop for RcMut<T> {
 }
 
 impl<T: Owned> Clone for RcMut<T> {
+    /// Return a shallow copy of the reference counted pointer.
     #[inline]
     fn clone(&self) -> RcMut<T> {
         unsafe {
@@ -209,11 +242,46 @@ impl<T: Owned> Clone for RcMut<T> {
     }
 }
 
+impl<T: Owned + DeepClone> DeepClone for RcMut<T> {
+    /// Return a deep copy of the reference counted pointer.
+    #[inline]
+    fn deep_clone(&self) -> RcMut<T> {
+        do self.with_borrow |x| {
+            // FIXME: #6497: should avoid freeze (slow)
+            RcMut::new(x.deep_clone())
+        }
+    }
+}
+
 #[cfg(test)]
 mod test_rc_mut {
     use super::*;
 
     #[test]
+    fn test_clone() {
+        let x = RcMut::new(5);
+        let y = x.clone();
+        do x.with_mut_borrow |value| {
+            *value = 20;
+        }
+        do y.with_borrow |value| {
+            assert_eq!(*value, 20);
+        }
+    }
+
+    #[test]
+    fn test_deep_clone() {
+        let x = RcMut::new(5);
+        let y = x.deep_clone();
+        do x.with_mut_borrow |value| {
+            *value = 20;
+        }
+        do y.with_borrow |value| {
+            assert_eq!(*value, 5);
+        }
+    }
+
+    #[test]
     fn borrow_many() {
         let x = RcMut::new(5);
         let y = x.clone();