about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/cell.rs7
-rw-r--r--src/libcore/clone.rs57
-rw-r--r--src/libcore/option.rs10
-rw-r--r--src/libcore/prelude.rs2
4 files changed, 66 insertions, 10 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index cacde5535db..87e8d0525e5 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -21,10 +21,17 @@ Similar to a mutable option type, but friendlier.
 */
 
 #[mutable]
+#[deriving(Clone)]
 pub struct Cell<T> {
     priv value: Option<T>
 }
 
+impl<T: DeepClone> DeepClone for Cell<T> {
+    fn deep_clone(&self) -> Cell<T> {
+        Cell{value: self.value.deep_clone()}
+    }
+}
+
 impl<T:cmp::Eq> cmp::Eq for Cell<T> {
     fn eq(&self, other: &Cell<T>) -> bool {
         (self.value) == (other.value)
diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs
index 9da970918b0..2ff860916c5 100644
--- a/src/libcore/clone.rs
+++ b/src/libcore/clone.rs
@@ -23,17 +23,12 @@ by convention implementing the `Clone` trait and calling the
 */
 
 pub trait Clone {
-    /// Return a deep copy of the owned object tree. Managed boxes are cloned with a shallow copy.
+    /// Return a deep copy of the owned object tree. Types with shared ownership like managed boxes
+    /// are cloned with a shallow copy.
     fn clone(&self) -> Self;
 }
 
-impl Clone for () {
-    /// Return a copy of the value.
-    #[inline(always)]
-    fn clone(&self) -> () { () }
-}
-
-impl<T:Clone> Clone for ~T {
+impl<T: Clone> Clone for ~T {
     /// Return a deep copy of the owned box.
     #[inline(always)]
     fn clone(&self) -> ~T { ~(**self).clone() }
@@ -54,7 +49,7 @@ impl<T> Clone for @mut T {
 macro_rules! clone_impl(
     ($t:ty) => {
         impl Clone for $t {
-            /// Return a copy of the value.
+            /// Return a deep copy of the value.
             #[inline(always)]
             fn clone(&self) -> $t { *self }
         }
@@ -77,9 +72,53 @@ clone_impl!(float)
 clone_impl!(f32)
 clone_impl!(f64)
 
+clone_impl!(())
 clone_impl!(bool)
 clone_impl!(char)
 
+pub trait DeepClone {
+    /// Return a deep copy of the object tree. Types with shared ownership are also copied via a
+    /// deep copy, unlike `Clone`. Note that this is currently unimplemented for managed boxes, as
+    /// it would need to handle cycles.
+    fn deep_clone(&self) -> Self;
+}
+
+macro_rules! deep_clone_impl(
+    ($t:ty) => {
+        impl DeepClone for $t {
+            /// Return a deep copy of the value.
+            #[inline(always)]
+            fn deep_clone(&self) -> $t { *self }
+        }
+    }
+)
+
+impl<T: DeepClone> DeepClone for ~T {
+    /// Return a deep copy of the owned box.
+    #[inline(always)]
+    fn deep_clone(&self) -> ~T { ~(**self).deep_clone() }
+}
+
+deep_clone_impl!(int)
+deep_clone_impl!(i8)
+deep_clone_impl!(i16)
+deep_clone_impl!(i32)
+deep_clone_impl!(i64)
+
+deep_clone_impl!(uint)
+deep_clone_impl!(u8)
+deep_clone_impl!(u16)
+deep_clone_impl!(u32)
+deep_clone_impl!(u64)
+
+deep_clone_impl!(float)
+deep_clone_impl!(f32)
+deep_clone_impl!(f64)
+
+deep_clone_impl!(())
+deep_clone_impl!(bool)
+deep_clone_impl!(char)
+
 #[test]
 fn test_owned_clone() {
     let a: ~int = ~5i;
diff --git a/src/libcore/option.rs b/src/libcore/option.rs
index 9aaa2921fe7..5aee3077e48 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -49,6 +49,7 @@ use num::Zero;
 use old_iter::{BaseIter, MutableIter, ExtendedIter};
 use old_iter;
 use str::StrSlice;
+use clone::DeepClone;
 
 #[cfg(test)] use str;
 
@@ -59,6 +60,15 @@ pub enum Option<T> {
     Some(T),
 }
 
+impl<T: DeepClone> DeepClone for Option<T> {
+    fn deep_clone(&self) -> Option<T> {
+        match *self {
+            Some(ref x) => Some(x.deep_clone()),
+            None => None
+        }
+    }
+}
+
 impl<T:Ord> Ord for Option<T> {
     fn lt(&self, other: &Option<T>) -> bool {
         match (self, other) {
diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs
index 22172db9302..4ed648161fc 100644
--- a/src/libcore/prelude.rs
+++ b/src/libcore/prelude.rs
@@ -27,7 +27,7 @@ pub use io::{print, println};
 
 /* Reexported types and traits */
 
-pub use clone::Clone;
+pub use clone::{Clone, DeepClone};
 pub use cmp::{Eq, ApproxEq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
 pub use container::{Container, Mutable, Map, Set};
 pub use hash::Hash;