@@ -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,46 @@ 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. with_borrow |cell| {
107
+ do value. with_mut_ref |inner| {
108
+ * inner = 20 ;
109
+ }
110
+ }
111
+ do y. with_borrow |value| {
112
+ assert_eq ! ( value. take( ) , 20 ) ;
113
+ }
114
+ }
115
+
116
+ #[ test]
117
+ fn test_deep_clone ( ) {
118
+ let x = Rc :: new ( Cell ( 5 ) ) ;
119
+ let y = x. deep_clone ( ) ;
120
+ do x. with_borrow |cell| {
121
+ do value. with_mut_ref |inner| {
122
+ * inner = 20 ;
123
+ }
124
+ }
125
+ do y. with_borrow |value| {
126
+ assert_eq ! ( value. take( ) , 5 ) ;
127
+ }
128
+ }
91
129
92
130
#[ test]
93
131
fn test_simple ( ) {
@@ -149,24 +187,26 @@ pub impl<T: Owned> RcMut<T> {
149
187
150
188
/// Fails if there is already a mutable borrow of the box
151
189
#[ inline]
152
- fn with_borrow ( & self , f : & fn ( & T ) ) {
190
+ fn with_borrow < U > ( & self , f : & fn ( & T ) -> U ) -> U {
153
191
unsafe {
154
192
assert ! ( ( * self . ptr) . borrow != Mutable ) ;
155
193
let previous = ( * self . ptr ) . borrow ;
156
194
( * self . ptr ) . borrow = Immutable ;
157
- f ( & ( * self . ptr ) . value ) ;
195
+ let res = f ( & ( * self . ptr ) . value ) ;
158
196
( * self . ptr ) . borrow = previous;
197
+ res
159
198
}
160
199
}
161
200
162
201
/// Fails if there is already a mutable or immutable borrow of the box
163
202
#[ inline]
164
- fn with_mut_borrow ( & self , f : & fn ( & mut T ) ) {
203
+ fn with_mut_borrow < U > ( & self , f : & fn ( & mut T ) -> U ) -> U {
165
204
unsafe {
166
205
assert ! ( ( * self . ptr) . borrow == Nothing ) ;
167
206
( * self . ptr ) . borrow = Mutable ;
168
- f ( & mut ( * self . ptr ) . value ) ;
207
+ let res = f ( & mut ( * self . ptr ) . value ) ;
169
208
( * self . ptr ) . borrow = Nothing ;
209
+ res
170
210
}
171
211
}
172
212
}
@@ -200,6 +240,7 @@ impl<T: Owned> Drop for RcMut<T> {
200
240
}
201
241
202
242
impl < T : Owned > Clone for RcMut < T > {
243
+ /// Return a shallow copy of the reference counted pointer.
203
244
#[ inline]
204
245
fn clone ( & self ) -> RcMut < T > {
205
246
unsafe {
@@ -209,10 +250,45 @@ impl<T: Owned> Clone for RcMut<T> {
209
250
}
210
251
}
211
252
253
+ impl < T : Owned + DeepClone > DeepClone for RcMut < T > {
254
+ /// Return a deep copy of the reference counted pointer.
255
+ #[ inline]
256
+ fn deep_clone ( & self ) -> RcMut < T > {
257
+ do self . with_borrow |x| {
258
+ // FIXME: #6497: should avoid freeze (slow)
259
+ RcMut :: new ( x. deep_clone ( ) )
260
+ }
261
+ }
262
+ }
263
+
212
264
#[ cfg( test) ]
213
265
mod test_rc_mut {
214
266
use super :: * ;
215
267
268
+ #[ test]
269
+ fn test_clone ( ) {
270
+ let x = RcMut :: new ( 5 ) ;
271
+ let y = x. clone ( ) ;
272
+ do x. with_mut_borrow |value| {
273
+ * value = 20 ;
274
+ }
275
+ do y. with_borrow |value| {
276
+ assert_eq ! ( * value, 20 ) ;
277
+ }
278
+ }
279
+
280
+ #[ test]
281
+ fn test_deep_clone ( ) {
282
+ let x = RcMut :: new ( 5 ) ;
283
+ let y = x. deep_clone ( ) ;
284
+ do x. with_mut_borrow |value| {
285
+ * value = 20 ;
286
+ }
287
+ do y. with_borrow |value| {
288
+ assert_eq ! ( * value, 5 ) ;
289
+ }
290
+ }
291
+
216
292
#[ test]
217
293
fn borrow_many ( ) {
218
294
let x = RcMut :: new ( 5 ) ;
0 commit comments