@@ -23,11 +23,17 @@ cycle cannot be created with `Rc<T>` because there is no way to modify it after
23
23
24
24
25
25
use std:: cast;
26
- use std:: libc:: { c_void, size_t, malloc, free} ;
27
26
use std:: ptr;
28
- use std:: sys;
29
27
use std:: unstable:: intrinsics;
30
28
29
+ // Convert ~T into *mut T without dropping it
30
+ #[ inline]
31
+ unsafe fn owned_to_raw < T > ( mut box : ~T ) -> * mut T {
32
+ let ptr = ptr:: to_mut_unsafe_ptr ( box) ;
33
+ intrinsics:: forget ( box) ;
34
+ ptr
35
+ }
36
+
31
37
struct RcBox < T > {
32
38
value : T ,
33
39
count : uint
@@ -42,21 +48,20 @@ pub struct Rc<T> {
42
48
43
49
impl < T > Rc < T > {
44
50
unsafe fn new ( value : T ) -> Rc < T > {
45
- let ptr = malloc ( sys:: size_of :: < RcBox < T > > ( ) as size_t ) as * mut RcBox < T > ;
46
- assert ! ( !ptr:: is_null( ptr) ) ;
47
- intrinsics:: move_val_init ( & mut * ptr, RcBox { value : value, count : 1 } ) ;
48
- Rc { ptr : ptr}
51
+ Rc { ptr : owned_to_raw ( ~RcBox { value : value, count : 1 } ) }
49
52
}
50
53
}
51
54
52
- // FIXME: #6516: should be a static method
53
- pub fn rc_from_owned < T : Send > ( value : T ) -> Rc < T > {
54
- unsafe { Rc :: new ( value) }
55
+ impl < T : Send > Rc < T > {
56
+ pub fn from_owned ( value : T ) -> Rc < T > {
57
+ unsafe { Rc :: new ( value) }
58
+ }
55
59
}
56
60
57
- // FIXME: #6516: should be a static method
58
- pub fn rc_from_const < T : Freeze > ( value : T ) -> Rc < T > {
59
- unsafe { Rc :: new ( value) }
61
+ impl < T : Freeze > Rc < T > {
62
+ pub fn from_const ( value : T ) -> Rc < T > {
63
+ unsafe { Rc :: new ( value) }
64
+ }
60
65
}
61
66
62
67
impl < T > Rc < T > {
@@ -73,8 +78,7 @@ impl<T> Drop for Rc<T> {
73
78
if self . ptr . is_not_null ( ) {
74
79
( * self . ptr ) . count -= 1 ;
75
80
if ( * self . ptr ) . count == 0 {
76
- ptr:: read_ptr ( self . ptr ) ;
77
- free ( self . ptr as * c_void )
81
+ let _: ~T = cast:: transmute ( self . ptr ) ;
78
82
}
79
83
}
80
84
}
@@ -107,7 +111,7 @@ mod test_rc {
107
111
108
112
#[ test]
109
113
fn test_clone( ) {
110
- let x = rc_from_owned ( Cell :: new ( 5 ) ) ;
114
+ let x = Rc :: from_owned ( Cell :: new ( 5 ) ) ;
111
115
let y = x. clone ( ) ;
112
116
do x. borrow ( ) . with_mut_ref |inner| {
113
117
* inner = 20 ;
@@ -117,7 +121,7 @@ mod test_rc {
117
121
118
122
#[ test]
119
123
fn test_deep_clone( ) {
120
- let x = rc_from_owned ( Cell :: new ( 5 ) ) ;
124
+ let x = Rc :: from_owned ( Cell :: new ( 5 ) ) ;
121
125
let y = x. deep_clone ( ) ;
122
126
do x. borrow ( ) . with_mut_ref |inner| {
123
127
* inner = 20 ;
@@ -127,31 +131,25 @@ mod test_rc {
127
131
128
132
#[ test]
129
133
fn test_simple ( ) {
130
- let x = rc_from_const ( 5 ) ;
134
+ let x = Rc :: from_const ( 5 ) ;
131
135
assert_eq ! ( * x. borrow( ) , 5 ) ;
132
136
}
133
137
134
138
#[ test]
135
139
fn test_simple_clone ( ) {
136
- let x = rc_from_const ( 5 ) ;
140
+ let x = Rc :: from_const ( 5 ) ;
137
141
let y = x. clone ( ) ;
138
142
assert_eq ! ( * x. borrow( ) , 5 ) ;
139
143
assert_eq ! ( * y. borrow( ) , 5 ) ;
140
144
}
141
145
142
146
#[ test]
143
147
fn test_destructor ( ) {
144
- let x = rc_from_owned ( ~5 ) ;
148
+ let x = Rc :: from_owned ( ~5 ) ;
145
149
assert_eq ! ( * * x. borrow( ) , 5 ) ;
146
150
}
147
151
}
148
152
149
- #[ abi = "rust-intrinsic" ]
150
- extern "rust-intrinsic" {
151
- fn init < T > ( ) -> T ;
152
- fn uninit < T > ( ) -> T ;
153
- }
154
-
155
153
#[ deriving( Eq ) ]
156
154
enum Borrow {
157
155
Mutable ,
@@ -175,21 +173,20 @@ pub struct RcMut<T> {
175
173
176
174
impl < T > RcMut < T > {
177
175
unsafe fn new ( value : T ) -> RcMut < T > {
178
- let ptr = malloc ( sys:: size_of :: < RcMutBox < T > > ( ) as size_t ) as * mut RcMutBox < T > ;
179
- assert ! ( !ptr:: is_null( ptr) ) ;
180
- intrinsics:: move_val_init ( & mut * ptr, RcMutBox { value : value, count : 1 , borrow : Nothing } ) ;
181
- RcMut { ptr : ptr}
176
+ RcMut { ptr : owned_to_raw ( ~RcMutBox { value : value, count : 1 , borrow : Nothing } ) }
182
177
}
183
178
}
184
179
185
- // FIXME: #6516: should be a static method
186
- pub fn rc_mut_from_owned < T : Send > ( value : T ) -> RcMut < T > {
187
- unsafe { RcMut :: new ( value) }
180
+ impl < T : Send > RcMut < T > {
181
+ pub fn from_owned ( value : T ) -> RcMut < T > {
182
+ unsafe { RcMut :: new ( value) }
183
+ }
188
184
}
189
185
190
- // FIXME: #6516: should be a static method
191
- pub fn rc_mut_from_const < T : Freeze > ( value : T ) -> RcMut < T > {
192
- unsafe { RcMut :: new ( value) }
186
+ impl < T : Freeze > RcMut < T > {
187
+ pub fn from_const ( value : T ) -> RcMut < T > {
188
+ unsafe { RcMut :: new ( value) }
189
+ }
193
190
}
194
191
195
192
impl < T > RcMut < T > {
@@ -226,8 +223,7 @@ impl<T> Drop for RcMut<T> {
226
223
if self . ptr . is_not_null ( ) {
227
224
( * self . ptr ) . count -= 1 ;
228
225
if ( * self . ptr ) . count == 0 {
229
- ptr:: replace_ptr ( self . ptr , uninit ( ) ) ;
230
- free ( self . ptr as * c_void )
226
+ let _: ~T = cast:: transmute ( self . ptr ) ;
231
227
}
232
228
}
233
229
}
@@ -262,7 +258,7 @@ mod test_rc_mut {
262
258
263
259
#[ test]
264
260
fn test_clone ( ) {
265
- let x = rc_mut_from_owned ( 5 ) ;
261
+ let x = RcMut :: from_owned ( 5 ) ;
266
262
let y = x. clone ( ) ;
267
263
do x. with_mut_borrow |value| {
268
264
* value = 20 ;
@@ -274,7 +270,7 @@ mod test_rc_mut {
274
270
275
271
#[ test]
276
272
fn test_deep_clone ( ) {
277
- let x = rc_mut_from_const ( 5 ) ;
273
+ let x = RcMut :: from_const ( 5 ) ;
278
274
let y = x. deep_clone ( ) ;
279
275
do x. with_mut_borrow |value| {
280
276
* value = 20 ;
@@ -286,7 +282,7 @@ mod test_rc_mut {
286
282
287
283
#[ test]
288
284
fn borrow_many ( ) {
289
- let x = rc_mut_from_owned ( 5 ) ;
285
+ let x = RcMut :: from_owned ( 5 ) ;
290
286
let y = x. clone ( ) ;
291
287
292
288
do x. with_borrow |a| {
@@ -302,7 +298,7 @@ mod test_rc_mut {
302
298
303
299
#[ test]
304
300
fn modify ( ) {
305
- let x = rc_mut_from_const ( 5 ) ;
301
+ let x = RcMut :: from_const ( 5 ) ;
306
302
let y = x. clone ( ) ;
307
303
308
304
do y. with_mut_borrow |a| {
@@ -317,22 +313,22 @@ mod test_rc_mut {
317
313
318
314
#[ test]
319
315
fn release_immutable ( ) {
320
- let x = rc_mut_from_owned ( 5 ) ;
316
+ let x = RcMut :: from_owned ( 5 ) ;
321
317
do x. with_borrow |_| { }
322
318
do x. with_mut_borrow |_| { }
323
319
}
324
320
325
321
#[ test]
326
322
fn release_mutable ( ) {
327
- let x = rc_mut_from_const ( 5 ) ;
323
+ let x = RcMut :: from_const ( 5 ) ;
328
324
do x. with_mut_borrow |_| { }
329
325
do x. with_borrow |_| { }
330
326
}
331
327
332
328
#[ test]
333
329
#[ should_fail]
334
330
fn frozen ( ) {
335
- let x = rc_mut_from_owned ( 5 ) ;
331
+ let x = RcMut :: from_owned ( 5 ) ;
336
332
let y = x. clone ( ) ;
337
333
338
334
do x. with_borrow |_| {
@@ -344,7 +340,7 @@ mod test_rc_mut {
344
340
#[ test]
345
341
#[ should_fail]
346
342
fn mutable_dupe ( ) {
347
- let x = rc_mut_from_const ( 5 ) ;
343
+ let x = RcMut :: from_const ( 5 ) ;
348
344
let y = x. clone ( ) ;
349
345
350
346
do x. with_mut_borrow |_| {
@@ -356,7 +352,7 @@ mod test_rc_mut {
356
352
#[ test]
357
353
#[ should_fail]
358
354
fn mutable_freeze ( ) {
359
- let x = rc_mut_from_owned ( 5 ) ;
355
+ let x = RcMut :: from_owned ( 5 ) ;
360
356
let y = x. clone ( ) ;
361
357
362
358
do x. with_mut_borrow |_| {
@@ -368,7 +364,7 @@ mod test_rc_mut {
368
364
#[ test]
369
365
#[ should_fail]
370
366
fn restore_freeze ( ) {
371
- let x = rc_mut_from_const ( 5 ) ;
367
+ let x = RcMut :: from_const ( 5 ) ;
372
368
let y = x. clone ( ) ;
373
369
374
370
do x. with_borrow |_| {
0 commit comments