@@ -22,6 +22,8 @@ by convention implementing the `Clone` trait and calling the
22
22
23
23
*/
24
24
25
+ use core:: kinds:: Const ;
26
+
25
27
pub trait Clone {
26
28
/// Return a deep copy of the owned object tree. Types with shared ownership like managed boxes
27
29
/// are cloned with a shallow copy.
@@ -78,11 +80,32 @@ clone_impl!(char)
78
80
79
81
pub trait DeepClone {
80
82
/// Return a deep copy of the object tree. Types with shared ownership are also copied via a
81
- /// deep copy, unlike `Clone`. Note that this is currently unimplemented for managed boxes, as
82
- /// it would need to handle cycles.
83
+ /// deep copy, unlike `Clone`.
83
84
fn deep_clone ( & self ) -> Self ;
84
85
}
85
86
87
+ impl < T : DeepClone > DeepClone for ~T {
88
+ /// Return a deep copy of the owned box.
89
+ #[ inline( always) ]
90
+ fn deep_clone ( & self ) -> ~T { ~( * * self ) . deep_clone ( ) }
91
+ }
92
+
93
+ // FIXME: #6525: should also be implemented for `T: Owned + DeepClone`
94
+ impl < T : Const + DeepClone > DeepClone for @T {
95
+ /// Return a deep copy of the managed box. The `Const` trait is required to prevent performing
96
+ /// a deep clone of a potentially cyclical type.
97
+ #[ inline( always) ]
98
+ fn deep_clone ( & self ) -> @T { @( * * self ) . deep_clone ( ) }
99
+ }
100
+
101
+ // FIXME: #6525: should also be implemented for `T: Owned + DeepClone`
102
+ impl < T : Const + DeepClone > DeepClone for @mut T {
103
+ /// Return a deep copy of the managed box. The `Const` trait is required to prevent performing
104
+ /// a deep clone of a potentially cyclical type.
105
+ #[ inline( always) ]
106
+ fn deep_clone ( & self ) -> @mut T { @mut ( * * self ) . deep_clone ( ) }
107
+ }
108
+
86
109
macro_rules! deep_clone_impl(
87
110
( $t: ty) => {
88
111
impl DeepClone for $t {
@@ -93,12 +116,6 @@ macro_rules! deep_clone_impl(
93
116
}
94
117
)
95
118
96
- impl < T : DeepClone > DeepClone for ~T {
97
- /// Return a deep copy of the owned box.
98
- #[ inline( always) ]
99
- fn deep_clone ( & self ) -> ~T { ~( * * self ) . deep_clone ( ) }
100
- }
101
-
102
119
deep_clone_impl ! ( int)
103
120
deep_clone_impl ! ( i8 )
104
121
deep_clone_impl ! ( i16 )
@@ -121,21 +138,29 @@ deep_clone_impl!(char)
121
138
122
139
#[ test]
123
140
fn test_owned_clone ( ) {
124
- let a: ~ int = ~5 i;
141
+ let a = ~5 i;
125
142
let b: ~int = a. clone ( ) ;
126
143
assert ! ( a == b) ;
127
144
}
128
145
129
146
#[ test]
130
147
fn test_managed_clone ( ) {
131
- let a: @ int = @5 i;
148
+ let a = @5 i;
132
149
let b: @int = a. clone ( ) ;
133
150
assert ! ( a == b) ;
134
151
}
135
152
153
+ #[ test]
154
+ fn test_managed_mut_deep_clone ( ) {
155
+ let x = @mut 5 i;
156
+ let y: @mut int = x. deep_clone ( ) ;
157
+ * x = 20 ;
158
+ assert_eq ! ( * y, 5 ) ;
159
+ }
160
+
136
161
#[ test]
137
162
fn test_managed_mut_clone ( ) {
138
- let a: @ mut int = @mut 5 i;
163
+ let a = @mut 5 i;
139
164
let b: @mut int = a. clone ( ) ;
140
165
assert ! ( a == b) ;
141
166
* b = 10 ;
0 commit comments