@@ -76,6 +76,7 @@ impl<T: Owned> Drop for Rc<T> {
76
76
77
77
78
78
impl < T : Owned > Clone for Rc < T > {
79
+ /// Return a shallow copy of the reference counted pointer.
79
80
#[ inline]
80
81
fn clone ( & self ) -> Rc < T > {
81
82
unsafe {
@@ -85,9 +86,38 @@ impl<T: Owned> Clone for Rc<T> {
85
86
}
86
87
}
87
88
89
+ impl < T : Owned + DeepClone > DeepClone for Rc < T > {
90
+ /// Return a deep copy of the reference counted pointer.
91
+ #[ inline]
92
+ fn deep_clone ( & self ) -> Rc < T > {
93
+ Rc :: new ( self . borrow ( ) . deep_clone ( ) )
94
+ }
95
+ }
96
+
88
97
#[ cfg( test) ]
89
98
mod test_rc {
90
99
use super :: * ;
100
+ use core:: cell:: Cell ;
101
+
102
+ #[ test]
103
+ fn test_clone( ) {
104
+ let x = Rc :: new ( Cell ( 5 ) ) ;
105
+ let y = x. clone ( ) ;
106
+ do x. borrow ( ) . with_mut_ref |inner| {
107
+ * inner = 20 ;
108
+ }
109
+ assert_eq ! ( y. borrow( ) . take( ) , 20 ) ;
110
+ }
111
+
112
+ #[ test]
113
+ fn test_deep_clone( ) {
114
+ let x = Rc :: new ( Cell ( 5 ) ) ;
115
+ let y = x. deep_clone ( ) ;
116
+ do x. borrow ( ) . with_mut_ref |inner| {
117
+ * inner = 20 ;
118
+ }
119
+ assert_eq ! ( y. borrow( ) . take( ) , 5 ) ;
120
+ }
91
121
92
122
#[ test]
93
123
fn test_simple ( ) {
@@ -96,7 +126,7 @@ mod test_rc {
96
126
}
97
127
98
128
#[ test]
99
- fn test_clone ( ) {
129
+ fn test_simple_clone ( ) {
100
130
let x = Rc :: new ( 5 ) ;
101
131
let y = x. clone ( ) ;
102
132
assert_eq ! ( * x. borrow( ) , 5 ) ;
@@ -149,24 +179,26 @@ pub impl<T: Owned> RcMut<T> {
149
179
150
180
/// Fails if there is already a mutable borrow of the box
151
181
#[ inline]
152
- fn with_borrow ( & self , f : & fn ( & T ) ) {
182
+ fn with_borrow < U > ( & self , f : & fn ( & T ) -> U ) -> U {
153
183
unsafe {
154
184
assert ! ( ( * self . ptr) . borrow != Mutable ) ;
155
185
let previous = ( * self . ptr ) . borrow ;
156
186
( * self . ptr ) . borrow = Immutable ;
157
- f ( & ( * self . ptr ) . value ) ;
187
+ let res = f ( & ( * self . ptr ) . value ) ;
158
188
( * self . ptr ) . borrow = previous;
189
+ res
159
190
}
160
191
}
161
192
162
193
/// Fails if there is already a mutable or immutable borrow of the box
163
194
#[ inline]
164
- fn with_mut_borrow ( & self , f : & fn ( & mut T ) ) {
195
+ fn with_mut_borrow < U > ( & self , f : & fn ( & mut T ) -> U ) -> U {
165
196
unsafe {
166
197
assert ! ( ( * self . ptr) . borrow == Nothing ) ;
167
198
( * self . ptr ) . borrow = Mutable ;
168
- f ( & mut ( * self . ptr ) . value ) ;
199
+ let res = f ( & mut ( * self . ptr ) . value ) ;
169
200
( * self . ptr ) . borrow = Nothing ;
201
+ res
170
202
}
171
203
}
172
204
}
@@ -200,6 +232,7 @@ impl<T: Owned> Drop for RcMut<T> {
200
232
}
201
233
202
234
impl < T : Owned > Clone for RcMut < T > {
235
+ /// Return a shallow copy of the reference counted pointer.
203
236
#[ inline]
204
237
fn clone ( & self ) -> RcMut < T > {
205
238
unsafe {
@@ -209,10 +242,45 @@ impl<T: Owned> Clone for RcMut<T> {
209
242
}
210
243
}
211
244
245
+ impl < T : Owned + DeepClone > DeepClone for RcMut < T > {
246
+ /// Return a deep copy of the reference counted pointer.
247
+ #[ inline]
248
+ fn deep_clone ( & self ) -> RcMut < T > {
249
+ do self . with_borrow |x| {
250
+ // FIXME: #6497: should avoid freeze (slow)
251
+ RcMut :: new ( x. deep_clone ( ) )
252
+ }
253
+ }
254
+ }
255
+
212
256
#[ cfg( test) ]
213
257
mod test_rc_mut {
214
258
use super :: * ;
215
259
260
+ #[ test]
261
+ fn test_clone ( ) {
262
+ let x = RcMut :: new ( 5 ) ;
263
+ let y = x. clone ( ) ;
264
+ do x. with_mut_borrow |value| {
265
+ * value = 20 ;
266
+ }
267
+ do y. with_borrow |value| {
268
+ assert_eq ! ( * value, 20 ) ;
269
+ }
270
+ }
271
+
272
+ #[ test]
273
+ fn test_deep_clone ( ) {
274
+ let x = RcMut :: new ( 5 ) ;
275
+ let y = x. deep_clone ( ) ;
276
+ do x. with_mut_borrow |value| {
277
+ * value = 20 ;
278
+ }
279
+ do y. with_borrow |value| {
280
+ assert_eq ! ( * value, 5 ) ;
281
+ }
282
+ }
283
+
216
284
#[ test]
217
285
fn borrow_many ( ) {
218
286
let x = RcMut :: new ( 5 ) ;
0 commit comments