16
16
use core:: atomic;
17
17
use core:: clone:: Clone ;
18
18
use core:: fmt:: { mod, Show } ;
19
+ use core:: cmp:: { Eq , Ord , PartialEq , PartialOrd , Ordering } ;
20
+ use core:: default:: Default ;
19
21
use core:: kinds:: { Sync , Send } ;
20
22
use core:: mem:: { min_align_of, size_of, drop} ;
21
23
use core:: mem;
22
24
use core:: ops:: { Drop , Deref } ;
23
25
use core:: option:: { Some , None , Option } ;
24
- use core:: ptr;
25
26
use core:: ptr:: RawPtr ;
27
+ use core:: ptr;
26
28
use heap:: deallocate;
27
29
28
30
/// An atomically reference counted wrapper for shared state.
@@ -92,16 +94,6 @@ impl<T: Sync + Send> Arc<T> {
92
94
Arc { _ptr : unsafe { mem:: transmute ( x) } }
93
95
}
94
96
95
- #[ inline]
96
- fn inner ( & self ) -> & ArcInner < T > {
97
- // This unsafety is ok because while this arc is alive we're guaranteed
98
- // that the inner pointer is valid. Furthermore, we know that the
99
- // `ArcInner` structure itself is `Sync` because the inner data is
100
- // `Sync` as well, so we're ok loaning out an immutable pointer to
101
- // these contents.
102
- unsafe { & * self . _ptr }
103
- }
104
-
105
97
/// Downgrades a strong pointer to a weak pointer.
106
98
///
107
99
/// Weak pointers will not keep the data alive. Once all strong references
@@ -115,8 +107,20 @@ impl<T: Sync + Send> Arc<T> {
115
107
}
116
108
}
117
109
110
+ impl < T > Arc < T > {
111
+ #[ inline]
112
+ fn inner ( & self ) -> & ArcInner < T > {
113
+ // This unsafety is ok because while this arc is alive we're guaranteed
114
+ // that the inner pointer is valid. Furthermore, we know that the
115
+ // `ArcInner` structure itself is `Sync` because the inner data is
116
+ // `Sync` as well, so we're ok loaning out an immutable pointer to
117
+ // these contents.
118
+ unsafe { & * self . _ptr }
119
+ }
120
+ }
121
+
118
122
#[ unstable = "waiting on stability of Clone" ]
119
- impl < T : Sync + Send > Clone for Arc < T > {
123
+ impl < T > Clone for Arc < T > {
120
124
/// Duplicate an atomically reference counted wrapper.
121
125
///
122
126
/// The resulting two `Arc` objects will point to the same underlying data
@@ -141,19 +145,13 @@ impl<T: Sync + Send> Clone for Arc<T> {
141
145
}
142
146
143
147
#[ experimental = "Deref is experimental." ]
144
- impl < T : Send + Sync > Deref < T > for Arc < T > {
148
+ impl < T > Deref < T > for Arc < T > {
145
149
#[ inline]
146
150
fn deref ( & self ) -> & T {
147
151
& self . inner ( ) . data
148
152
}
149
153
}
150
154
151
- impl < T : Send + Sync + Show > Show for Arc < T > {
152
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
153
- ( * * self ) . fmt ( f)
154
- }
155
- }
156
-
157
155
impl < T : Send + Sync + Clone > Arc < T > {
158
156
/// Acquires a mutable pointer to the inner contents by guaranteeing that
159
157
/// the reference count is one (no sharing is possible).
@@ -279,6 +277,38 @@ impl<T: Sync + Send> Drop for Weak<T> {
279
277
}
280
278
}
281
279
280
+ #[ unstable = "waiting on PartialEq" ]
281
+ impl < T : PartialEq > PartialEq for Arc < T > {
282
+ fn eq ( & self , other : & Arc < T > ) -> bool { * ( * self ) == * ( * other) }
283
+ fn ne ( & self , other : & Arc < T > ) -> bool { * ( * self ) != * ( * other) }
284
+ }
285
+ #[ unstable = "waiting on PartialOrd" ]
286
+ impl < T : PartialOrd > PartialOrd for Arc < T > {
287
+ fn partial_cmp ( & self , other : & Arc < T > ) -> Option < Ordering > {
288
+ ( * * self ) . partial_cmp ( & * * other)
289
+ }
290
+ fn lt ( & self , other : & Arc < T > ) -> bool { * ( * self ) < * ( * other) }
291
+ fn le ( & self , other : & Arc < T > ) -> bool { * ( * self ) <= * ( * other) }
292
+ fn ge ( & self , other : & Arc < T > ) -> bool { * ( * self ) >= * ( * other) }
293
+ fn gt ( & self , other : & Arc < T > ) -> bool { * ( * self ) > * ( * other) }
294
+ }
295
+ #[ unstable = "waiting on Ord" ]
296
+ impl < T : Ord > Ord for Arc < T > {
297
+ fn cmp ( & self , other : & Arc < T > ) -> Ordering { ( * * self ) . cmp ( & * * other) }
298
+ }
299
+ #[ unstable = "waiting on Eq" ]
300
+ impl < T : Eq > Eq for Arc < T > { }
301
+
302
+ impl < T : fmt:: Show > fmt:: Show for Arc < T > {
303
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
304
+ ( * * self ) . fmt ( f)
305
+ }
306
+ }
307
+
308
+ impl < T : Default + Sync + Send > Default for Arc < T > {
309
+ fn default ( ) -> Arc < T > { Arc :: new ( Default :: default ( ) ) }
310
+ }
311
+
282
312
#[ cfg( test) ]
283
313
#[ allow( experimental) ]
284
314
mod tests {
@@ -440,4 +470,8 @@ mod tests {
440
470
let a = Arc :: new ( 5u32 ) ;
441
471
assert ! ( format!( "{}" , a) . as_slice( ) == "5" )
442
472
}
473
+
474
+ // Make sure deriving works with Arc<T>
475
+ #[ deriving( Eq , Ord , PartialEq , PartialOrd , Clone , Show , Default ) ]
476
+ struct Foo { inner : Arc < int > }
443
477
}
0 commit comments