39
39
//! }
40
40
//!
41
41
//! // The methods for working with our currently defined static logger
42
- //! static_atomic_ref! {
43
- //! static LOGGER: AtomicRef<LoggerInfo>;
44
- //! }
42
+ //! static LOGGER: AtomicRef<LoggerInfo> = AtomicRef::new(None);
45
43
//! fn log(msg: &str) -> bool {
46
44
//! if let Some(info) = LOGGER.load(Ordering::SeqCst) {
47
45
//! info.logger.log(msg);
73
71
//! ```
74
72
#![ no_std]
75
73
76
-
77
74
use core:: sync:: atomic:: { AtomicPtr , Ordering } ;
78
75
use core:: marker:: PhantomData ;
79
76
use core:: fmt;
@@ -92,91 +89,11 @@ pub struct AtomicRef<'a, T: 'a> {
92
89
// `#![feature(const_fn)]`
93
90
struct Invariant < ' a , T : ' a > ( & ' a mut & ' a mut T ) ;
94
91
95
- /// You will probably never need to use this type. It exists mostly for internal
96
- /// use in the `static_atomic_ref!` macro.
97
- ///
98
- /// Unlike `AtomicUsize` and its ilk, we cannot have an `ATOMIC_REF_INIT` const
99
- /// which is initialized to `None`, as constants cannot be generic over a type
100
- /// parameter. This is the same reason why `AtomicPtr` does not have an
101
- /// `ATOMIC_PTR_INIT` const.
102
- ///
103
- /// Instead, we have a single const for `&'static u8`, and take advantage of the
104
- /// fact that all AtomicRef types have identical layout to implement the
105
- /// `static_atomic_ref!` macro.
106
- ///
107
- /// Please use `static_atomic_ref!` instead of this constant if you need to
108
- /// implement a static atomic reference variable.
109
- pub const ATOMIC_U8_REF_INIT : AtomicRef < ' static , u8 > = AtomicRef {
110
- data : AtomicPtr :: new ( null_mut ( ) ) ,
111
- _marker : PhantomData ,
112
- } ;
113
-
114
92
/// Re-export `core` for `static_atomic_ref!` (which may be used in a
115
93
/// non-`no_std` crate, where `core` is unavailable).
116
94
#[ doc( hidden) ]
117
95
pub use core:: { mem as core_mem, ops as core_ops} ;
118
96
119
- /// A macro to define a statically allocated `AtomicRef<'static, T>` which is
120
- /// initialized to `None`.
121
- ///
122
- /// # Examples
123
- ///
124
- /// ```
125
- /// # #[macro_use]
126
- /// # extern crate atomic_ref;
127
- /// use std::sync::atomic::Ordering;
128
- ///
129
- /// static_atomic_ref! {
130
- /// static SOME_REFERENCE: AtomicRef<i32>;
131
- /// pub static PUB_REFERENCE: AtomicRef<u64>;
132
- /// }
133
- ///
134
- /// fn main() {
135
- /// let a: Option<&'static i32> = SOME_REFERENCE.load(Ordering::SeqCst);
136
- /// assert_eq!(a, None);
137
- /// }
138
- /// ```
139
- #[ macro_export]
140
- macro_rules! static_atomic_ref {
141
- ( $( #[ $attr: meta] ) * static $N: ident : AtomicRef <$T: ty>; $( $t: tt) * ) => {
142
- static_atomic_ref!( @PRIV , $( #[ $attr] ) * static $N : $T; $( $t) * ) ;
143
- } ;
144
- ( $( #[ $attr: meta] ) * pub static $N: ident : AtomicRef <$T: ty>; $( $t: tt) * ) => {
145
- static_atomic_ref!( @PUB , $( #[ $attr] ) * static $N : $T; $( $t) * ) ;
146
- } ;
147
- ( @$VIS: ident, $( #[ $attr: meta] ) * static $N: ident : $T: ty; $( $t: tt) * ) => {
148
- static_atomic_ref!( @MAKE TY , $VIS, $( #[ $attr] ) * , $N) ;
149
- impl $crate:: core_ops:: Deref for $N {
150
- type Target = $crate:: AtomicRef <' static , $T>;
151
- #[ allow( unsafe_code) ]
152
- fn deref<' a>( & ' a self ) -> & ' a $crate:: AtomicRef <' static , $T> {
153
- static STORAGE : $crate:: AtomicRef <' static , u8 > = $crate:: ATOMIC_U8_REF_INIT ;
154
- unsafe { $crate:: core_mem:: transmute( & STORAGE ) }
155
- }
156
- }
157
- static_atomic_ref!( $( $t) * ) ;
158
- } ;
159
- ( @MAKE TY , PUB , $( #[ $attr: meta] ) * , $N: ident) => {
160
- #[ allow( missing_copy_implementations) ]
161
- #[ allow( non_camel_case_types) ]
162
- #[ allow( dead_code) ]
163
- $( #[ $attr] ) *
164
- pub struct $N { _private: ( ) }
165
- #[ doc( hidden) ]
166
- pub static $N: $N = $N { _private: ( ) } ;
167
- } ;
168
- ( @MAKE TY , PRIV , $( #[ $attr: meta] ) * , $N: ident) => {
169
- #[ allow( missing_copy_implementations) ]
170
- #[ allow( non_camel_case_types) ]
171
- #[ allow( dead_code) ]
172
- $( #[ $attr] ) *
173
- struct $N { _private: ( ) }
174
- #[ doc( hidden) ]
175
- static $N: $N = $N { _private: ( ) } ;
176
- } ;
177
- ( ) => ( ) ;
178
- }
179
-
180
97
/// An internal helper function for converting `Option<&'a T>` values to
181
98
/// `*mut T` for storing in the `AtomicUsize`.
182
99
const fn from_opt < ' a , T > ( p : Option < & ' a T > ) -> * mut T {
@@ -416,10 +333,9 @@ impl<'a, T> Default for AtomicRef<'a, T> {
416
333
#[ cfg( test) ]
417
334
mod tests {
418
335
use core:: sync:: atomic:: Ordering ;
336
+ use super :: AtomicRef ;
419
337
420
- static_atomic_ref ! {
421
- static FOO : AtomicRef <i32 >;
422
- }
338
+ static FOO : AtomicRef < i32 > = AtomicRef :: new ( None ) ;
423
339
424
340
static A : i32 = 10 ;
425
341
0 commit comments