@@ -14,10 +14,6 @@ The `Rc` type provides shared ownership of an immutable value. Destruction is de
14
14
will occur as soon as the last owner is gone. It is marked as non-sendable because it avoids the
15
15
overhead of atomic reference counting.
16
16
17
- The `RcMut` type provides shared ownership of a mutable value. Since multiple owners prevent
18
- inherited mutability, a dynamic freezing check is used to maintain the invariant that an `&mut`
19
- reference is a unique handle and the type is marked as non-`Freeze`.
20
-
21
17
*/
22
18
23
19
use ptr:: RawPtr ;
@@ -151,236 +147,3 @@ mod test_rc {
151
147
assert_eq ! ( * * x. borrow( ) , 5 ) ;
152
148
}
153
149
}
154
-
155
- #[ deriving( Eq ) ]
156
- enum Borrow {
157
- Mutable ,
158
- Immutable ,
159
- Nothing
160
- }
161
-
162
- struct RcMutBox < T > {
163
- value : T ,
164
- count : uint ,
165
- borrow : Borrow
166
- }
167
-
168
- /// Mutable reference counted pointer type
169
- #[ no_send]
170
- #[ no_freeze]
171
- #[ unsafe_no_drop_flag]
172
- pub struct RcMut < T > {
173
- priv ptr: * mut RcMutBox < T > ,
174
- }
175
-
176
- impl < T : Freeze > RcMut < T > {
177
- /// Construct a new mutable reference-counted box from a `Freeze` value
178
- #[ inline]
179
- pub fn new ( value : T ) -> RcMut < T > {
180
- unsafe { RcMut :: new_unchecked ( value) }
181
- }
182
- }
183
-
184
- impl < T : Send > RcMut < T > {
185
- /// Construct a new mutable reference-counted box from a `Send` value
186
- #[ inline]
187
- pub fn from_send ( value : T ) -> RcMut < T > {
188
- unsafe { RcMut :: new_unchecked ( value) }
189
- }
190
- }
191
-
192
- impl < T > RcMut < T > {
193
- /// Unsafety construct a new mutable reference-counted box from any value.
194
- ///
195
- /// It is possible to create cycles, which will leak, and may interact
196
- /// poorly with managed pointers.
197
- #[ inline]
198
- pub unsafe fn new_unchecked ( value : T ) -> RcMut < T > {
199
- RcMut { ptr : transmute ( ~RcMutBox { value : value, count : 1 , borrow : Nothing } ) }
200
- }
201
- }
202
-
203
- impl < T > RcMut < T > {
204
- /// Fails if there is already a mutable borrow of the box
205
- #[ inline]
206
- pub fn with_borrow < U > ( & self , f: |& T | -> U ) -> U {
207
- unsafe {
208
- assert ! ( ( * self . ptr) . borrow != Mutable ) ;
209
- let previous = ( * self . ptr ) . borrow ;
210
- ( * self . ptr ) . borrow = Immutable ;
211
- let res = f ( & ( * self . ptr ) . value ) ;
212
- ( * self . ptr ) . borrow = previous;
213
- res
214
- }
215
- }
216
-
217
- /// Fails if there is already a mutable or immutable borrow of the box
218
- #[ inline]
219
- pub fn with_mut_borrow < U > ( & self , f: |& mut T | -> U ) -> U {
220
- unsafe {
221
- assert_eq ! ( ( * self . ptr) . borrow, Nothing ) ;
222
- ( * self . ptr ) . borrow = Mutable ;
223
- let res = f ( & mut ( * self . ptr ) . value ) ;
224
- ( * self . ptr ) . borrow = Nothing ;
225
- res
226
- }
227
- }
228
- }
229
-
230
- #[ unsafe_destructor]
231
- impl < T > Drop for RcMut < T > {
232
- fn drop ( & mut self ) {
233
- unsafe {
234
- if self . ptr . is_not_null ( ) {
235
- ( * self . ptr ) . count -= 1 ;
236
- if ( * self . ptr ) . count == 0 {
237
- let _: ~RcMutBox < T > = transmute ( self . ptr ) ;
238
- }
239
- }
240
- }
241
- }
242
- }
243
-
244
- impl < T > Clone for RcMut < T > {
245
- /// Return a shallow copy of the reference counted pointer.
246
- #[ inline]
247
- fn clone ( & self ) -> RcMut < T > {
248
- unsafe {
249
- ( * self . ptr ) . count += 1 ;
250
- RcMut { ptr : self . ptr }
251
- }
252
- }
253
- }
254
-
255
- impl < T : DeepClone > DeepClone for RcMut < T > {
256
- /// Return a deep copy of the reference counted pointer.
257
- #[ inline]
258
- fn deep_clone ( & self ) -> RcMut < T > {
259
- do self . with_borrow |x| {
260
- // FIXME: #6497: should avoid freeze (slow)
261
- unsafe { RcMut :: new_unchecked ( x. deep_clone ( ) ) }
262
- }
263
- }
264
- }
265
-
266
- #[ cfg( test) ]
267
- mod test_rc_mut {
268
- use super :: * ;
269
-
270
- #[ test]
271
- fn test_clone ( ) {
272
- let x = RcMut :: from_send ( 5 ) ;
273
- let y = x. clone ( ) ;
274
- do x. with_mut_borrow |value| {
275
- * value = 20 ;
276
- }
277
- do y. with_borrow |value| {
278
- assert_eq ! ( * value, 20 ) ;
279
- }
280
- }
281
-
282
- #[ test]
283
- fn test_deep_clone ( ) {
284
- let x = RcMut :: new ( 5 ) ;
285
- let y = x. deep_clone ( ) ;
286
- do x. with_mut_borrow |value| {
287
- * value = 20 ;
288
- }
289
- do y. with_borrow |value| {
290
- assert_eq ! ( * value, 5 ) ;
291
- }
292
- }
293
-
294
- #[ test]
295
- fn borrow_many ( ) {
296
- let x = RcMut :: from_send ( 5 ) ;
297
- let y = x. clone ( ) ;
298
-
299
- do x. with_borrow |a| {
300
- assert_eq ! ( * a, 5 ) ;
301
- do y. with_borrow |b| {
302
- assert_eq ! ( * b, 5 ) ;
303
- do x. with_borrow |c| {
304
- assert_eq ! ( * c, 5 ) ;
305
- }
306
- }
307
- }
308
- }
309
-
310
- #[ test]
311
- fn modify ( ) {
312
- let x = RcMut :: new ( 5 ) ;
313
- let y = x. clone ( ) ;
314
-
315
- do y. with_mut_borrow |a| {
316
- assert_eq ! ( * a, 5 ) ;
317
- * a = 6 ;
318
- }
319
-
320
- do x. with_borrow |a| {
321
- assert_eq ! ( * a, 6 ) ;
322
- }
323
- }
324
-
325
- #[ test]
326
- fn release_immutable ( ) {
327
- let x = RcMut :: from_send ( 5 ) ;
328
- do x. with_borrow |_| { }
329
- do x. with_mut_borrow |_| { }
330
- }
331
-
332
- #[ test]
333
- fn release_mutable ( ) {
334
- let x = RcMut :: new ( 5 ) ;
335
- do x. with_mut_borrow |_| { }
336
- do x. with_borrow |_| { }
337
- }
338
-
339
- #[ test]
340
- #[ should_fail]
341
- fn frozen ( ) {
342
- let x = RcMut :: from_send ( 5 ) ;
343
- let y = x. clone ( ) ;
344
-
345
- do x. with_borrow |_| {
346
- do y. with_mut_borrow |_| {
347
- }
348
- }
349
- }
350
-
351
- #[ test]
352
- #[ should_fail]
353
- fn mutable_dupe ( ) {
354
- let x = RcMut :: new ( 5 ) ;
355
- let y = x. clone ( ) ;
356
-
357
- do x. with_mut_borrow |_| {
358
- do y. with_mut_borrow |_| {
359
- }
360
- }
361
- }
362
-
363
- #[ test]
364
- #[ should_fail]
365
- fn mutable_freeze ( ) {
366
- let x = RcMut :: from_send ( 5 ) ;
367
- let y = x. clone ( ) ;
368
-
369
- do x. with_mut_borrow |_| {
370
- do y. with_borrow |_| {
371
- }
372
- }
373
- }
374
-
375
- #[ test]
376
- #[ should_fail]
377
- fn restore_freeze ( ) {
378
- let x = RcMut :: new ( 5 ) ;
379
- let y = x. clone ( ) ;
380
-
381
- do x. with_borrow |_| {
382
- do x. with_borrow |_| { }
383
- do y. with_mut_borrow |_| { }
384
- }
385
- }
386
- }
0 commit comments