@@ -67,6 +67,51 @@ cfg_if! {
67
67
use std:: ops:: Add ;
68
68
use std:: panic:: { resume_unwind, catch_unwind, AssertUnwindSafe } ;
69
69
70
+ /// This is a single threaded variant of AtomicCell provided by crossbeam.
71
+ /// Unlike `Atomic` this is intended for all `Copy` types,
72
+ /// but it lacks the explicit ordering arguments.
73
+ #[ derive( Debug ) ]
74
+ pub struct AtomicCell <T : Copy >( Cell <T >) ;
75
+
76
+ impl <T : Copy > AtomicCell <T > {
77
+ #[ inline]
78
+ pub fn new( v: T ) -> Self {
79
+ AtomicCell ( Cell :: new( v) )
80
+ }
81
+
82
+ #[ inline]
83
+ pub fn get_mut( & mut self ) -> & mut T {
84
+ self . 0 . get_mut( )
85
+ }
86
+ }
87
+
88
+ impl <T : Copy > AtomicCell <T > {
89
+ #[ inline]
90
+ pub fn into_inner( self ) -> T {
91
+ self . 0 . into_inner( )
92
+ }
93
+
94
+ #[ inline]
95
+ pub fn load( & self ) -> T {
96
+ self . 0 . get( )
97
+ }
98
+
99
+ #[ inline]
100
+ pub fn store( & self , val: T ) {
101
+ self . 0 . set( val)
102
+ }
103
+
104
+ #[ inline]
105
+ pub fn swap( & self , val: T ) -> T {
106
+ self . 0 . replace( val)
107
+ }
108
+ }
109
+
110
+ /// This is a single threaded variant of `AtomicU64`, `AtomicUsize`, etc.
111
+ /// It differs from `AtomicCell` in that it has explicit ordering arguments
112
+ /// and is only intended for use with the native atomic types.
113
+ /// You should use this type through the `AtomicU64`, `AtomicUsize`, etc, type aliases
114
+ /// as it's not intended to be used separately.
70
115
#[ derive( Debug ) ]
71
116
pub struct Atomic <T : Copy >( Cell <T >) ;
72
117
@@ -77,7 +122,8 @@ cfg_if! {
77
122
}
78
123
}
79
124
80
- impl <T : Copy + PartialEq > Atomic <T > {
125
+ impl <T : Copy > Atomic <T > {
126
+ #[ inline]
81
127
pub fn into_inner( self ) -> T {
82
128
self . 0 . into_inner( )
83
129
}
@@ -92,10 +138,14 @@ cfg_if! {
92
138
self . 0 . set( val)
93
139
}
94
140
141
+ #[ inline]
95
142
pub fn swap( & self , val: T , _: Ordering ) -> T {
96
143
self . 0 . replace( val)
97
144
}
145
+ }
98
146
147
+ impl <T : Copy + PartialEq > Atomic <T > {
148
+ #[ inline]
99
149
pub fn compare_exchange( & self ,
100
150
current: T ,
101
151
new: T ,
@@ -113,6 +163,7 @@ cfg_if! {
113
163
}
114
164
115
165
impl <T : Add <Output =T > + Copy > Atomic <T > {
166
+ #[ inline]
116
167
pub fn fetch_add( & self , val: T , _: Ordering ) -> T {
117
168
let old = self . 0 . get( ) ;
118
169
self . 0 . set( old + val) ;
@@ -271,6 +322,8 @@ cfg_if! {
271
322
272
323
pub use std:: sync:: atomic:: { AtomicBool , AtomicUsize , AtomicU32 , AtomicU64 } ;
273
324
325
+ pub use crossbeam_utils:: atomic:: AtomicCell ;
326
+
274
327
pub use std:: sync:: Arc as Lrc ;
275
328
pub use std:: sync:: Weak as Weak ;
276
329
0 commit comments