@@ -67,6 +67,51 @@ cfg_if! {
6767 use std:: ops:: Add ;
6868 use std:: panic:: { resume_unwind, catch_unwind, AssertUnwindSafe } ;
6969
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.
70115 #[ derive( Debug ) ]
71116 pub struct Atomic <T : Copy >( Cell <T >) ;
72117
@@ -77,7 +122,8 @@ cfg_if! {
77122 }
78123 }
79124
80- impl <T : Copy + PartialEq > Atomic <T > {
125+ impl <T : Copy > Atomic <T > {
126+ #[ inline]
81127 pub fn into_inner( self ) -> T {
82128 self . 0 . into_inner( )
83129 }
@@ -92,10 +138,14 @@ cfg_if! {
92138 self . 0 . set( val)
93139 }
94140
141+ #[ inline]
95142 pub fn swap( & self , val: T , _: Ordering ) -> T {
96143 self . 0 . replace( val)
97144 }
145+ }
98146
147+ impl <T : Copy + PartialEq > Atomic <T > {
148+ #[ inline]
99149 pub fn compare_exchange( & self ,
100150 current: T ,
101151 new: T ,
@@ -113,6 +163,7 @@ cfg_if! {
113163 }
114164
115165 impl <T : Add <Output =T > + Copy > Atomic <T > {
166+ #[ inline]
116167 pub fn fetch_add( & self , val: T , _: Ordering ) -> T {
117168 let old = self . 0 . get( ) ;
118169 self . 0 . set( old + val) ;
@@ -271,6 +322,8 @@ cfg_if! {
271322
272323 pub use std:: sync:: atomic:: { AtomicBool , AtomicUsize , AtomicU32 , AtomicU64 } ;
273324
325+ pub use crossbeam_utils:: atomic:: AtomicCell ;
326+
274327 pub use std:: sync:: Arc as Lrc ;
275328 pub use std:: sync:: Weak as Weak ;
276329
0 commit comments