73
73
//! ```
74
74
#![ no_std]
75
75
76
- use core:: sync:: atomic:: { AtomicUsize , ATOMIC_USIZE_INIT , Ordering } ;
76
+ use core:: sync:: atomic:: { AtomicPtr , Ordering } ;
77
77
use core:: marker:: PhantomData ;
78
78
use core:: fmt;
79
+ use core:: ptr:: null_mut;
79
80
use core:: default:: Default ;
80
81
81
82
/// A mutable Option<&'a, T> type which can be safely shared between threads.
82
83
#[ repr( C ) ]
83
84
pub struct AtomicRef < ' a , T : ' a > {
84
- data : AtomicUsize ,
85
+ data : AtomicPtr < T > ,
85
86
// Make `AtomicRef` invariant over `'a` and `T`
86
87
_marker : PhantomData < & ' a mut & ' a mut T > ,
87
88
}
@@ -101,7 +102,7 @@ pub struct AtomicRef<'a, T: 'a> {
101
102
/// Please use `static_atomic_ref!` instead of this constant if you need to
102
103
/// implement a static atomic reference variable.
103
104
pub const ATOMIC_U8_REF_INIT : AtomicRef < ' static , u8 > = AtomicRef {
104
- data : ATOMIC_USIZE_INIT ,
105
+ data : AtomicPtr :: new ( null_mut ( ) ) ,
105
106
_marker : PhantomData ,
106
107
} ;
107
108
@@ -171,19 +172,19 @@ macro_rules! static_atomic_ref {
171
172
( ) => ( ) ;
172
173
}
173
174
174
- /// An internal helper function for converting `Option<&'a T>` values to usize
175
- /// for storing in the `AtomicUsize`.
176
- fn from_opt < ' a , T > ( p : Option < & ' a T > ) -> usize {
175
+ /// An internal helper function for converting `Option<&'a T>` values to
176
+ /// `*mut T` for storing in the `AtomicUsize`.
177
+ fn from_opt < ' a , T > ( p : Option < & ' a T > ) -> * mut T {
177
178
match p {
178
- Some ( p) => p as * const T as usize ,
179
- None => 0 ,
179
+ Some ( p) => p as * const T as * mut T ,
180
+ None => null_mut ( ) ,
180
181
}
181
182
}
182
183
183
- /// An internal helper function for converting `usize ` values stored in the
184
+ /// An internal helper function for converting `*mut T ` values stored in the
184
185
/// `AtomicUsize` back into `Option<&'a T>` values.
185
- unsafe fn to_opt < ' a , T > ( p : usize ) -> Option < & ' a T > {
186
- ( p as * const T ) . as_ref ( )
186
+ unsafe fn to_opt < ' a , T > ( p : * mut T ) -> Option < & ' a T > {
187
+ p . as_ref ( )
187
188
}
188
189
189
190
impl < ' a , T > AtomicRef < ' a , T > {
@@ -199,7 +200,7 @@ impl<'a, T> AtomicRef<'a, T> {
199
200
/// ```
200
201
pub fn new ( p : Option < & ' a T > ) -> AtomicRef < ' a , T > {
201
202
AtomicRef {
202
- data : AtomicUsize :: new ( from_opt ( p) ) ,
203
+ data : AtomicPtr :: new ( from_opt ( p) ) ,
203
204
_marker : PhantomData ,
204
205
}
205
206
}
0 commit comments