@@ -48,12 +48,21 @@ impl<T: Freeze> Rc<T> {
48
48
}
49
49
}
50
50
51
+ impl < T : Send > Rc < T > {
52
+ /// Construct a new reference-counted box from a `Send` value
53
+ #[ inline]
54
+ pub fn from_send ( value : T ) -> Rc < T > {
55
+ unsafe {
56
+ Rc :: new_unchecked ( value)
57
+ }
58
+ }
59
+ }
60
+
51
61
impl < T > Rc < T > {
52
62
/// Unsafety construct a new reference-counted box from any value.
53
63
///
54
- /// If the type is not `Freeze`, the `Rc` box will incorrectly still be considered as a `Freeze`
55
- /// type. It is also possible to create cycles, which will leak, and may interact poorly with
56
- /// managed pointers.
64
+ /// It is possible to create cycles, which will leak, and may interact
65
+ /// poorly with managed pointers.
57
66
#[ inline]
58
67
pub unsafe fn new_unchecked ( value : T ) -> Rc < T > {
59
68
Rc { ptr : transmute ( ~RcBox { value : value, count : 1 } ) }
@@ -104,26 +113,22 @@ mod test_rc {
104
113
105
114
#[ test]
106
115
fn test_clone( ) {
107
- unsafe {
108
- let x = Rc :: new_unchecked ( Cell :: new ( 5 ) ) ;
109
- let y = x. clone ( ) ;
110
- do x. borrow ( ) . with_mut_ref |inner| {
111
- * inner = 20 ;
112
- }
113
- assert_eq ! ( y. borrow( ) . take( ) , 20 ) ;
116
+ let x = Rc :: from_send ( Cell :: new ( 5 ) ) ;
117
+ let y = x. clone ( ) ;
118
+ do x. borrow ( ) . with_mut_ref |inner| {
119
+ * inner = 20 ;
114
120
}
121
+ assert_eq ! ( y. borrow( ) . take( ) , 20 ) ;
115
122
}
116
123
117
124
#[ test]
118
125
fn test_deep_clone( ) {
119
- unsafe {
120
- let x = Rc :: new_unchecked ( Cell :: new ( 5 ) ) ;
121
- let y = x. deep_clone ( ) ;
122
- do x. borrow ( ) . with_mut_ref |inner| {
123
- * inner = 20 ;
124
- }
125
- assert_eq ! ( y. borrow( ) . take( ) , 5 ) ;
126
+ let x = Rc :: from_send ( Cell :: new ( 5 ) ) ;
127
+ let y = x. deep_clone ( ) ;
128
+ do x. borrow ( ) . with_mut_ref |inner| {
129
+ * inner = 20 ;
126
130
}
131
+ assert_eq ! ( y. borrow( ) . take( ) , 5 ) ;
127
132
}
128
133
129
134
#[ test]
@@ -142,10 +147,8 @@ mod test_rc {
142
147
143
148
#[ test]
144
149
fn test_destructor ( ) {
145
- unsafe {
146
- let x = Rc :: new_unchecked ( ~5 ) ;
147
- assert_eq ! ( * * x. borrow( ) , 5 ) ;
148
- }
150
+ let x = Rc :: from_send ( ~5 ) ;
151
+ assert_eq ! ( * * x. borrow( ) , 5 ) ;
149
152
}
150
153
}
151
154
0 commit comments